【发布时间】:2017-03-31 22:59:50
【问题描述】:
example.component.ts 中的可观察函数
public name: string;
this._cm.getData().subscribe(
response => {
this.name = response.name;
},
error => {
this.name = undefined;
}
模拟服务
public _cm = {
data: {
name: 'test'
}
getData(): Observable<any> {
return Observable.of(this.data);
}
}
在 example.component.spec.ts 中测试 observable 函数
// this test work corectly
it('Should set name property to "test"', () => {
comp._cm.getData();
expect(comp.name).toBe('test');
}
it('Should set name property to undefined', () => {
comp._cm.getData();
expect(comp.name).toBe('undefined');
}
但我不知道如何模拟错误响应getData()
【问题讨论】:
标签: unit-testing testing angular typescript karma-runner