我正在学习角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);
}
}最后,我想测试实践应用程序中的所有功能。但到目前为止,我只想让测试通过,这些测试是由角cli产生的。
从我从文档中读到的资料来看,我正在做的事情看起来是正确的。
发布于 2019-02-14 14:03:33
添加迟到的答案,因为我有同样的错误,但它是由另一个问题。在我的例子中,我测试了一个异步调用:
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(() => {编辑V10
从角版本10开始,async已被废弃,并已被waitForAsync所取代。因此,新版本的代码将是
import { waitForAsync} from '@angular/core/testing';
it('can test for 404 error', waitForAsync(() => {https://stackoverflow.com/questions/41431039
复制相似问题