【问题标题】:Unit Testing Promises in AngularJS Error: Expected a spy, but got getCtrl({ })AngularJS 错误中的单元测试承诺:预期是间谍,但得到了 getCtrl({ })
【发布时间】:2016-08-04 07:17:05
【问题描述】:

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

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

__spyOn(getProduct, 'getproductDetailsPull').and.returnValue(deferred.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;
        };
    }

控制器代码

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

        });

    }); 

【问题讨论】:

    标签: angularjs unit-testing testing jasmine


    【解决方案1】:

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

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

    间谍应该是 int he format spyOn(object, "methodName"),所以在你的情况下:

    spyOn(getProductService, 'getproductDetailsPull')
    

    考虑为你的承诺这样做:

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

    【讨论】:

      猜你喜欢
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-12-20
      • 1970-01-01
      • 2018-06-07
      • 1970-01-01
      相关资源
      最近更新 更多