I have ngResourceMockFactory that 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 embed this in my tests in beforeEach like this:
beforeEach(inject(function(NgResourceMock) { ngResourceMock = new NgResourceMock(); }));
then I use it as follows:
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 see the following error:
TypeError: 'undefined' is not an object (evaluating 'context.prototype.$promise')
This makes me think that something is wrong with my ngResourceMockFactory , but I'm not sure what it is.
javascript angularjs jasmine karma-jasmine
Matt dionis
source share