'expect' 在没有当前规范时使用
'expect' was used when there was no current spec
我正在学习 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.
生成的测试
根据我从文档中读到的内容,我正在做的事情看起来是正确的。
expect()
语句出现在 it()
语句中,如下所示:
describe('ExperimentsComponent',() => {
...
it('should be created', () => {
expect(component).toBeTruthy();
});
...
}
错误是这样读的:
ExperimentsComponent should be created was false
您似乎混淆了 describe
和 it
参数
添加迟到的答案,因为我有同样的错误,但它是由另一个问题引起的。在我的例子中,我正在测试一个异步调用:
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');
}
);
});
因此从 angular 测试中添加异步方法解决了问题:
import { async } from '@angular/core/testing';
it('can test for 404 error', async(() => {
编辑 V10
从 angular 版本 10 开始,async
现已弃用并已替换为 waitForAsync
。所以新版本的代码是
import { waitForAsync} from '@angular/core/testing';
it('can test for 404 error', waitForAsync(() => {
我正在学习 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.
生成的测试根据我从文档中读到的内容,我正在做的事情看起来是正确的。
expect()
语句出现在 it()
语句中,如下所示:
describe('ExperimentsComponent',() => {
...
it('should be created', () => {
expect(component).toBeTruthy();
});
...
}
错误是这样读的:
ExperimentsComponent should be created was false
您似乎混淆了 describe
和 it
参数
添加迟到的答案,因为我有同样的错误,但它是由另一个问题引起的。在我的例子中,我正在测试一个异步调用:
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');
}
);
});
因此从 angular 测试中添加异步方法解决了问题:
import { async } from '@angular/core/testing';
it('can test for 404 error', async(() => {
编辑 V10
从 angular 版本 10 开始,async
现已弃用并已替换为 waitForAsync
。所以新版本的代码是
import { waitForAsync} from '@angular/core/testing';
it('can test for 404 error', waitForAsync(() => {