我正在尝试使用Jasmine 2.2.0:CheckoutService.patch()来模拟服务,该服务会将承诺返回到我的CheckoutController中:

describe('CheckoutController', function() {
    var $scope, vm, checkoutServiceMock, def;

    beforeEach(function(){
        module('app');

        checkoutServiceMock = jasmine.createSpyObj('CheckoutService', ['patch']);

        inject(function($rootScope, $controller, $q){
            def = $q.defer();

            $scope = $rootScope.$new();

            vm = $controller('CheckoutController', {
                $scope: $scope,
                CheckoutService: checkoutServiceMock
            });

            checkoutServiceMock.patch.andReturn(def.promise);
        });
   });
});


但我得到:TypeError: 'undefined' is not a function (evaluating 'checkoutServiceMock.patch.andReturn(def.promise)')

我缺少什么?

最佳答案

我猜您应该将andReturn更改为and.callfake(),例如:

spyOn(checkoutServiceMock, "patch").and.callFake(function() {
  return def.promise;
});


有关Jasmine 2.2,请参见docs

10-05 20:43
查看更多