【发布时间】:2018-11-12 22:03:17
【问题描述】:
我有一项服务正在返回多个 http 调用的 forkjoin。我想测试这个场景。
class CommentService{
addComments(){
let ob1 = Observable.of({});
let ob2 = Observable.of({});
if(any condition)
ob1 = {this.http.post('/url/1')};
if(any condition)
ob2 = {this.http.post('/url/2'};
return Observable.forkJoin(ob1,ob2)
}
}
上面是我的服务类。我如何模拟 http 调用。
describe("CommentService", () => {
let httpClient: HttpClient;
let httpTestingController: HttpTestingController;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [HttpClientModule, HttpClientTestingModule],
providers: [CommentService]
});
httpClient = TestBed.get(HttpClient);
httpTestingController = TestBed.get(HttpTestingController);
});
it('addComments() call with normal and gate', inject([CommentService], (service: CommentService) => {
let cmts = service.addComments();
const reqGateComment = httpTestingController.expectOne('/url/1');
expect(reqGateComment.request.method).toEqual('POST');
const reqFactComment = httpTestingController.expectOne('/url/2');
expect(reqFactComment.request.method).toEqual('POST');
reqGateComment.flush({});
reqFactComment.flush({});
httpTestingController.verify();
cmts.subscribe(results=>{
expect(results.length).toEqual(2);
});
}));
});
我得到以下测试失败。 CommentService addFactsAndComments() 调用正常和门
错误:预期有一个条件匹配请求“匹配 URL:
'/url/1", found none.
【问题讨论】:
-
这方面有什么更新吗?
标签: angular typescript rxjs karma-jasmine angular-httpclient