Mocking ngResource in Angular unit tests

2019-06-21 12:55发布

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.

2条回答
聊天终结者
2楼-- · 2019-06-21 13:20

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.

查看更多
迷人小祖宗
3楼-- · 2019-06-21 13:43

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.

查看更多
登录 后发表回答