我查看了doc,以了解如何测试http请求,但是我无法从文档中找出如何对服务进行单元测试,如下所示。
class service {
data$: BehaviorSubject<any> = new BehaviorSubject<>(any);
getDatas(): Observable<any> {
this.http.get(url).subscribe((response) => {
this.data$.emit(response);
});
// if return a observable will be like:
// return this.http.get(url).
}
}
如果方法未返回Observable,如何测试上面的服务?
答案 0 :(得分:1)
The Head Rush的评论是正确的。这是我方便使用的代码示例。
服务
export class SomeService {
constructor() {}
goDoSomething(): void {
console.log('I did something');
}
}
测试
describe('testing goDoSomething method', () => {
it('should call console log', () => {
spyOn(console, 'log'); // console is the object, and log is the method I expect to be called
service.goDoSomething();
expect(console.log).toHaveBeenCalled();
});
});