【发布时间】:2019-01-05 10:02:24
【问题描述】:
我正在尝试为以下函数编写一个测试用例:
foo = () => {
this.someService.getDetails({key:'value'}).subscribe(details => {
//do stuff
this.someService.getMoreDetails().subscribe(moreDetails => {
//do stuff
});
});
}
服务如下所示:
getDetails = (args) :Observable<any> {
return this.http.post<any>(//calls)
}
// similar for getMoreDetails
我写的测试文件是这样的:
const someServiceStub = jasmine.createSpyObj('someService', ['getDetails', 'getMoreDetails']);
...
...
it('should called getMoreDetails', () => {
component.foo();
fixture.detectChanges();
someServiceStub.getDetails.and.returnValue(Observable.of
({ Details: 'Tired of giving you details'})
);
expect(someServiceStub.getMoreDetails).toHaveBeenCalled();
});
但是,我的测试用例未能给出错误“无法读取未定义的属性订阅”(对于 foo 函数内的第一行)。
我也尝试过使用 mockservice 类,但出现了同样的错误。 这可能是什么原因,我该如何解决?
【问题讨论】:
标签: angular unit-testing jasmine karma-runner