Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 306
MattDionis Avatar asked Oct 05 '16 14:10

MattDionis


People also ask

What is mocking in unit testing Angular?

Introduction. Mocking is a great idea for testing Angular apps because it makes maintenance easier and helps reduce future bugs. There are a few complex tools, such as XUnit, for mocking an Angular CLI project. You can execute the mocking methods described in this guide only if you use vanilla Jasmine + Angular Testbed ...

How do you mock a component in Angular unit testing?

A mock component in Angular tests can be created by MockComponent function. The mock component respects the interface of its original component, but all its methods are dummies. To create a mock component, simply pass its class into MockComponent function.

What can be mocked for unit testing?

What is mocking? Mocking is a process used in unit testing when the unit being tested has external dependencies. The purpose of mocking is to isolate and focus on the code being tested and not on the behavior or state of external dependencies.

Should unit test mock dependencies?

Correct. You should mock things that depend on anything persistent or external in order to prevent the test from depending on anything persistent or external.


2 Answers

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.

like image 21
Pritish Vaidya Avatar answered Sep 28 '22 11:09

Pritish Vaidya


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.

like image 126
Aibu Avatar answered Sep 28 '22 12:09

Aibu