MattDionis
MattDionis

Reputation: 3616

Mocking ngResource in Angular unit tests

I have an ngResourceMockFactory which looks like this:

(function() {
  'use strict';

  angular.module('app')
    .factory('NgResourceMock', ngResourceMockFactory)
  ;

  ngResourceMockFactory.$inject = [];

  function ngResourceMockFactory() {
    function NgResourceMock() {
      var context = this;

      context.$promise.then = function() {
        context.prototype.$promise.then.apply(context, arguments);
      };

      context.$promise.finally = function() {
        context.prototype.$promise.finally.apply(context, arguments);
      };
    }

    NgResourceMock.prototype.$promise = {
      then: function(onSuccess, onError) {
        this.$promise.onSuccess = onSuccess;
        this.$promise.onError = onError;
      },
      finally: function(onComplete) {
        this.$promise.onComplete = onComplete;
      }
    };

    return NgResourceMock;
  }
})();

I inject this into my tests in a beforeEach like so:

beforeEach(inject(function(NgResourceMock) {
  ngResourceMock = new NgResourceMock();
}));

then I use it like this:

describe('initiateWorkflow function', function() {
  beforeEach(function() {
    vm.player = {id: 123};
    spyOn(dataService, 'initiateWorkflow').and.returnValue(ngResourceMock);
    vm.initiateWorkflow();
  });

  it('should call dataService.initiateWorkflow', function() {
    expect(dataService.initiateWorkflow).toHaveBeenCalledWith({playerId: vm.player.id}, {});
  });
});

but I keep seeing the following error:

TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise')

This leads me to believe that something is wrong with my ngResourceMockFactory, but I'm not sure what it is.

Upvotes: 5

Views: 498

Answers (2)

Pritish Vaidya
Pritish Vaidya

Reputation: 22189

Here is the solution to your problem.

The error TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise') is caused when you try to invoke the promise object before invoking the function into which it is defined or into which your parent function is defined.

Here the returnValue(ngResourceMock) is directly calling into the function without the context and parameters need to be defined.

Therefore you can try to add another beforeEach statement like

beforeEach(angular.mock.module(app));

to load your app module

Here may be the same concept related to your problem another link here.

Hope it may help you a bit.

Upvotes: 2

Aibu
Aibu

Reputation: 401

Don't know if this can be of any help, but if you are trying to evaluate asynchronous operations in your tests, you may want to use the done() method in Jasmine.

As per their documentation:

beforeEach(function(done) {
    setTimeout(function() {
      value = 0;
      done();
    }, 1);
});

by passing done as a parameter of the beforeEach callback, any test run after the before each will wait until the done() function has been called.

Source: Jasmine (Asynchronous Support section).

Hope this helps.

Upvotes: 4

Related Questions