gpt4 book ai didi

angularjs - AngularJS 中的单元测试 promise 错误 : Expected a spy, 但得到了 getCtrl({ })

转载 作者:行者123 更新时间:2023-11-28 20:39:40 25 4
gpt4 key购买 nike

我创建了通过 httppost 请求返回产品详细信息的服务我有 Controller ,通过它调用服务 __getProductService.getproductDetailsPull().then(function(response){__然后我在 Controller 中获取数据

我通过注入(inject) spy 在 jasmine-karma 中为此编写了一个测试用例

__spyOn(getProduct, 'getproductDetailsPull').and.returnValue(deferred.promise);__

**但是我得到了 promise 的错误**

错误 1

Expected a spy, but got deleteCtrl({ }).

错误2

.then is not a function

服务代码

   var myapp = angular.module('abcservice');
myapp.service('getProductService',function($http,$q){

var productDetails = [];
var productResponse = null;

this.setproduct= function() {
var obj = {
adminId : 15,
productOrderID: 174824929577
};
if (this.productResponse == null) {
this.productResponse = $http.post('someurl',obj).success(function(data, status, headers,config) {
this.productResponse = mapJson(data);
}).error(function(data, status, headers,config)
{
console.log("error while fetching data from spring controller:" +error);
});
}
return this.productResponse;
};

this.getproductDetailsPull = function(productResponse) {
return this.productResponse;
};
}

Controller 代码

angular
.module('getCtrl', []);
getCtrl.$inject = ['$scope', '$http', '$rootScope', 'getProductService'];
function getCtrl($scope, $http, $rootScope, getProductService) {

getProductService.getproductDetailsPull().then(function(response){


$scope.displayData = response.data.productorder;
$scope.lineItemData = response.data.OrderItem;

}
}

Jasmine 测试用例

 describe('getCtrl Test', function() {

var $scope = null;
var $getProduct = null;
var $rootScope = null;
var deferred,$q;

beforeEach(module('abcservice','getCtrl'));

beforeEach(inject(function (_$controller_,$rootScope,getProduct,_$q_) {
$controller = _$controller_;
$scope = $rootScope.$new();
$q = _$q_;;
deferred = _$q_.defer();

spyOn(getProduct, 'getproductDetailsPull').and.returnValue(deferred.promise);

controller = $controller('getCtrl', { $scope: $scope,$rootScope: $rootScope,getProduct:getProduct });
}));

it('Exists controller, function() {

expect(controller).toHaveBeenCalled();

});

});

最佳答案

您打错了,getProduct 不是您的服务名称。您需要像这样注入(inject)服务:

 beforeEach(inject(function (_$controller_,$rootScope,getProductService

spy 应该是 int 他格式 spyOn(object, "methodName"),所以在你的情况下:

spyOn(getProductService, 'getproductDetailsPull')

为了您的 promise 考虑这样做:

spyOn(getProductService, 'getproductDetailsPull').and.returnValue($q.when())

您的测试用例有点奇怪,我假设您这样做只是为了让事情正常进行,但您可能想要这样的东西:

it('Product is fetched, function() { 
scope.$digest(); // if your using $q you need this (maybe move it to before call)
expect(getProductService.getproductDetailsPull).toHaveBeenCalled();
});

关于angularjs - AngularJS 中的单元测试 promise 错误 : Expected a spy, 但得到了 getCtrl({ }),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38760798/

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