gpt4 book ai didi

javascript - 如何让 angularJS 在 jasmine 测试中使用 $http 和 $resource 的实际实现?

转载 作者:行者123 更新时间:2023-11-28 07:50:52 25 4
gpt4 key购买 nike

我有一些 Jasmine 单元测试,它们是“集成测试”,它们运行一些编写为 Angular 服务的核心组件。但我的组件不会调用网络。我证明问题的两个测试是这样的......

[顺便说一句,我有一个禁用 CORS 的插件]

  it('test http get', function () {

httpGet('http://endpoint.json');

function httpGet(theUrl) {
var xmlHttp = null;

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", theUrl, false);
xmlHttp.send(null);
return xmlHttp.responseText;
}

})

it('test angular http get', inject(function($http) {

$http.get('http://endpoint.json').
success(function (data, status, headers, config) {

console.log('success');

}).
error(function (data, status, headers, config) {

console.log('failure');

});

}));

我知道这是因为 $httpBackend 模拟。但是我需要使用真正的实现。我想使用 jasmine 的异步功能来等待响应或超时,但我只是不知道如何让 Angular 真正进行调用。

在其他新的 jQuery 调用中工作正常! :-)

最佳答案

$httpBackend仅演示请求,旨在节省您的时间。

Why?

假设您想测试您的函数是否正常工作,但该函数取决于从服务器检索的数据。

Instead of testing the honesty of the data, and the $http.get method which would take some time, it tests the honesty of the function, making sure that your function is doing what it is supposed to do.

现在,为了创建异步调用,您可以与 $timeout 或 $interval 服务集成,

或者

使用 Protractor :e2e - 端到端测试工具,“作为用户”测试您的应用程序,它能够测试 DOM 元素内值的诚实性,例如:

点击<button id="a"></button> ->验证 <p id="b">值为 hello world .

<强> Protractor

关于javascript - 如何让 angularJS 在 jasmine 测试中使用 $http 和 $resource 的实际实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26818385/

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