当没有当前规格时,使用'expect'

问题描述 投票:4回答:2

我正在学习Angular 2测试,我收到一个目前对我没有意义的错误。

'expect' was used when there was no current spec,

测试:

import {ExperimentsComponent} from "./experiments.component";
import {StateService} from "../common/state.service";
import {ExperimentsService} from "../common/experiments.service";

describe('experiments.component title and body should be correct',() => {

  let stateService = StateService;
  let experimentService = ExperimentsService;

  let app = new ExperimentsComponent(new stateService, new experimentService);

  expect(app.title).toBe('Experiments Page');
  expect(app.body).toBe('This is the about experiments body');

});

组件:

import {Component, OnInit} from "@angular/core";
import {Experiment} from "../common/experiment.model";
import {ExperimentsService} from "../common/experiments.service";
import {StateService} from "../common/state.service";


@Component({
    selector: 'experiments',
    template: require('./experiments.component.html'),

})
export class ExperimentsComponent implements OnInit {
    title: string = 'Experiments Page';
    body: string = 'This is the about experiments body';
    message: string;
    experiments: Experiment[];

    constructor(private _stateService: StateService,
                private _experimentsService: ExperimentsService) {
    }

    ngOnInit() {
        this.experiments = this._experimentsService.getExperiments();
        this.message = this._stateService.getMessage();
    }

    updateMessage(m: string): void {
        this._stateService.setMessage(m);
    }
}

最终我想测试练习应用程序中的所有功能。但截至目前,我只是通过angular-cli生成的测试通过。

从我从文档中读到的内容看起来我正在做的事情是正确的。

angular typescript angular2-testing
2个回答
6
投票

expect()语句出现在it()语句中,如下所示:

describe('ExperimentsComponent',() => {
...
  it('should be created', () => {
    expect(component).toBeTruthy();
  });
...
}

这就是错误的读取方式:

应该创建的ExperimentsComponent为false

你似乎有describeit参数混淆


1
投票

添加迟到的答案,因为我有同样的错误,但它是由另一个问题引起的。在我的情况下,我正在测试异步调用:

  it('can test for 404 error', () => {
    const emsg = `'products' with id='9999999' not found`;

    productService.getProduct(9999999).subscribe( <-- Async call made
      () => {
        fail('should have failed with the 404 error');
      },
      error => {
        expect(error.status).toEqual(404, 'status');
        expect(error.body.error).toEqual(emsg, 'error');
      }
    );
  });

因此,从角度测试添加异步方法解决了这个问题:

  import { async } from '@angular/core/testing';

  it('can test for 404 error', async(() => {
© www.soinside.com 2019 - 2024. All rights reserved.