gpt4 book ai didi

javascript - 全局 BeforeEach 用于使用 Mocha 和 Angular 模拟 HTTP 请求

转载 作者:行者123 更新时间:2023-11-30 12:13:42 25 4
gpt4 key购买 nike

我在 module.run 中触发的请求很少:

angular.module('demo').run(function($http) {
$http.get('/some/thing');
$http.get('/some/other/thing');
});

当我在测试中使用 $rootScope.$apply 来解决模拟 promise 时,我得到 unexpected request errors for '/some/thing''/some/other/thing'

修复它的一种方法是在 beforeach 中设置 $httpBackend:

$httpBackend.when('GET', mockData.API_URL + '/some/thing').respond(200, {});
$httpBackend.when('GET', mockData.API_URL + '/some/other/thing').respond(200, {});

这会起作用,但这意味着我必须将它放入我使用 $rootScope.$apply 的每个测试文件之前。

如何为每个测试文件设置全局的 $httpBackend 配置?
或者对于这个问题有更好的解决方案吗?

最佳答案

来自Mocha website ,在“根级 Hook ”:

You may also pick any file and add "root"-level hooks. For example, add beforeEach() outside of all describe() blocks. This will cause the callback to beforeEach() to run before any test case, regardless of the file it lives in (this is because Mocha has a hidden describe() block, called the "root suite").

beforeEach(function() {   
console.log('before every test in every file');
});

关于javascript - 全局 BeforeEach 用于使用 Mocha 和 Angular 模拟 HTTP 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33043438/

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