I'm having trouble using injectAsync
with the http.MockBackend
. The auth.ngOnInit()
method calls Http.get()
, but in this test the MockBackend.connections.toPromise().then()
method is never called:
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. When I run the test, however, it fails with 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()
. 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;
}));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.