gpt4 book ai didi

javascript - Jasmine - 模拟 spy On.and.callFake http成功错误

转载 作者:行者123 更新时间:2023-12-03 06:17:58 24 4
gpt4 key购买 nike

在我的 Controller 中,我调用了 http 服务。这是我的 Controller :

myApp.controller('MyController', function MyController($scope, myData) {
myData.getItemsByPage(1, 10)
.success(function (data, status, headers, config) {
$scope.items = data.items;
})
.error(function (data, status, headers, config) {
var test = 1;
});
});

这是 Jasmine 测试:

it('it should return results', function () {
var results = {
"items": [
{"Title": "Star Wars1","Year": "1977"},
{"Title": "Star Wars2","Year": "1980"},
{"Title": "Star Wars3","Year": "1983"}]
};

spyOn(myData, 'getItemsByPage').and.callFake(function (message, success, fail) {
return {
success: function (successCallback) {
var status = {};
var headers = function() {
return {
'x-pagination': '{"totalCount": 3}'
}
};
var config = {};

successCallback(results, status, headers, config);
},
error: function (fail) {
var x = 1;
}
}
});
$rootScope.$apply();
$controller('MyController', { $scope: $scope });
});

我遇到了异常“TypeError:无法获取未定义或空引用的属性‘错误’”

最佳答案

出于某种原因,您必须在调用 success 方法后返回一个错误对象。

spyOn(myData, 'getItemsByPage').and.callFake(function (message, success, fail) 
{
return {
success: function (successCallback) {
var status = {};
var headers = function() {
return {
'x-pagination': '{"totalCount": 3}'
}
};
var config = {};

successCallback(results, status, headers, config);

return {
error: function (errorCallback) {
var x = 1;
}
}
},
error: function (fail) {
var x = 1;
}
}
});

关于javascript - Jasmine - 模拟 spy On.and.callFake http成功错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39000688/

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