我正在学习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生成的测试通过。
从我从文档中读到的内容看起来我正在做的事情是正确的。
答案 0 :(得分:6)
expect()
语句出现在it()
语句中,如下所示:
describe('ExperimentsComponent',() => {
...
it('should be created', () => {
expect(component).toBeTruthy();
});
...
}
了解错误的方法:
ExperimentsComponent应该是create created is false
您似乎混淆了describe
和it
参数
答案 1 :(得分: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(() => {