gpt4 book ai didi

javascript - Angular2测试异步Http.MockBackend.connections promise 不会产生

转载 作者:行者123 更新时间:2023-11-28 06:10:05 26 4
gpt4 key购买 nike

我在使用 injectAsynchttp.MockBackend 时遇到问题。 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;
}));

我已在调试器中确认正在调用 MockBackend.connections.next() 方法。但是,当我运行测试时,它失败并显示错误:超时 - 在 jasmine.DEFAULT_TIMEOUT_INTERVAL 指定的超时内未调用异步回调。我在这里缺少什么?

最佳答案

感谢@alxhub和@ericmartinezr in gitter ,问题是我需要将可观察范围缩小到单个结果,然后才能调用 toPromise()。所以这有效:

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;
}));

关于javascript - Angular2测试异步Http.MockBackend.connections promise 不会产生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36488690/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com