[英]Angular2 test async Http.MockBackend.connections promise not yielding
I'm having trouble using injectAsync
with the http.MockBackend
. 我在使用injectAsync
和http.MockBackend
遇到了麻烦。 The auth.ngOnInit()
method calls Http.get()
, but in this test the MockBackend.connections.toPromise().then()
method is never called: auth.ngOnInit()
方法调用Http.get()
,但在此测试中,永远不会调用MockBackend.connections.toPromise().then()
方法:
it('should check if the user is authenticated',
injectAsync([Auth, MockBackend], (auth, backend) => {
let promise = backend.connections.toPromise().then(
(connection) => {
let link = document.createElement('a');
link.href = connection.request.url;
expect(link.pathname).toBe('/api/auth/user/');
});
auth.ngOnInit();
return promise;
}));
I've confirmed in the debugger that the MockBackend.connections.next()
method is being called. 我已在调试器中确认正在调用MockBackend.connections.next()
方法。 When I run the test, however, it fails with Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
但是,当我运行测试时,它失败并显示Error: Timeout - Async callback was not invoked within timeout specified by jasmine.DEFAULT_TIMEOUT_INTERVAL.
What am I missing here? 我在这里错过了什么?
Thanks to @alxhub and @ericmartinezr in gitter , the issue is that I need to narrow the observable to a single result before I can call toPromise()
. 感谢@alxhub和@ericmartinezr 在gitter中 ,问题是我需要在我可以调用toPromise()
之前将observable缩小到单个结果。 So this works: 这样可行:
it('should check if the user is authenticated',
injectAsync([Auth, MockBackend], (auth, backend) => {
let promise = backend.connections.first().toPromise().then(
(connection) => {
let link = document.createElement('a');
link.href = connection.request.url;
expect(link.pathname).toBe('/api/auth/user/');
});
auth.ngOnInit();
return promise;
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.