4

I thought I made good headway by coding a Jasmine mock like this. But I couldn't fix this error.

How does the spyOn method actually work ? In fact this method seems to widely used. Has something changed in 2.0

describe('Test Controller', function() {

var $scope, $rootScope, $q, controller, mockService, queryDeferred;

//What is the expected response ?
var expectedResponse = [{ suffix: 'Mr.'}, { prefix: 'Miss'}];

beforeEach(function () {
  angular.module("Test", []);
});

beforeEach(module('Test'));

beforeEach(inject(function(_$rootScope_, _$q_, $controller) {
      $q = _$q_;
      $rootScope = _$rootScope_;
      $scope = $rootScope.$new();


      mockService = {
        query: function() {
          queryDeferred = $q.defer();
          return queryDeferred.promise;
        }
      }


      spyOn(mockService, 'query').andCallThrough();

      // inject the mocked Service into the controller.
      controller = $controller('TestController', {
        '$scope': $scope,
        'ServiceApi': mockService
      });



}));

});


TypeError: undefined is not a function
        at Object.<anonymous> (d:/karma-requirejs-Angular/test/PromiseSpec.j
s:34:39)
        at Object.invoke (d:/karma-requirejs-Angular/lib/angular/angular.js:
4152:17)
        at Object.workFn (d:/karma-requirejs-Angular/lib/angular-mocks/angul
ar-mocks.js:2255:20)
    Error: Declaration Location
        at window.inject.angular.mock.inject (d:/karma-requirejs-Angular/lib
/angular-mocks/angular-mocks.js:2226:25)
        at Suite.<anonymous> (d:/karma-requirejs-Angular/test/PromiseSpec.js
:20:16)
        at d:/karma-requirejs-Angular/test/PromiseSpec.js:7:3
        at Object.context.execCb (d:/karma-requirejs-Angular/node_modules/re
 quirejs/require.js:1658:33)
        at Object.Module.check (d:/karma-requirejs-Angular/node_modules/requ

Update :

I am posting this working code as an answer. I fixed a few issues.

describe('Test Controller', function() {

    var $q,
        $rootScope,
        $scope,
        mockService,
        mockResponse = [{ suffix: 'Mr.'}, { prefix: 'Miss'}];



    beforeEach(module('myApp'));

    beforeEach(inject(function(_$q_,
                               _$rootScope_) {
        $q = _$q_;
        $rootScope = _$rootScope_;
    }));

    beforeEach(inject(function($controller) {
        $scope = $rootScope.$new();

        mockService = {
            query: function() {
                queryDeferred = $q.defer();
                return {$promise: queryDeferred.promise};
            }
        }

         spyOn(mockService, 'query').and.callThrough();

         $controller('TestCtrl', {
           '$scope': $scope,
           'testService': mockService
         });

    }));

    describe('Given a setup for the invocation of the mock query', function() {
            beforeEach(function() {
                queryDeferred.resolve(mockResponse);
                $rootScope.$apply();
            });

           it('When the call to the mock service is issued ', function() {

               resolvespy = jasmine.createSpy('resolve');
               rejectspy = jasmine.createSpy('reject');

               expect(mockService.query).toHaveBeenCalled();

               var q = $scope.getPromise();

               q.then(resolvespy,rejectspy);

               queryDeferred.resolve(mockResponse);
               $rootScope.$apply();


               expect(resolvespy).toHaveBeenCalledWith(mockResponse);
               expect(rejectspy).not.toHaveBeenCalled();

           });

           it('Then the mock service should have returned the correct response', function() {

              expect($scope.test).toBe(mockResponse);
          });

            it('Then the mock service should have returned the correct response', function() {

                expect($scope.testfunction).toBeDefined();
            });

            it("should do something else when something happens", function() {
                $scope.testfunction();
                expect($scope.value).toBe('value');
            });
      });

});

1 Answer 1

20

WIth Jasmine 2.0 you should replace:

andCallThrough();

with:

and.callThrough();

See: Jasmine 2.0 documentation and Upgrading to 2.0

Sign up to request clarification or add additional context in comments.

1 Comment

I updated my question with my working code. I made the change suggested by you too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.