Matheus Lima
Matheus Lima

Reputation: 2143

Mocking a Service into Controller

I'm trying to mock a service, with Jasmine 2.2.0: CheckoutService.patch() which returns a promise into my 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);
        });
   });
});

But I'm getting: TypeError: 'undefined' is not a function (evaluating 'checkoutServiceMock.patch.andReturn(def.promise)')

What I am missing?

Upvotes: 1

Views: 112

Answers (1)

Aurelio
Aurelio

Reputation: 25812

I guess that you should change andReturn to and.callfake(), for example:

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

See docs for Jasmine 2.2.

Upvotes: 2

Related Questions