Sajith
Sajith

Reputation: 106

Unknown provider error when testing a angular factory using jasmine

I am trying to test a angular "service" (actually implemented using factory) using jasmine, and getting the following error:

collectionService should contain collectionService
Error: [$injector:unpr] Unknown provider: collectionServiceProvider <- collectionService http://errors.angularjs.org/1.2.1/$injector/unpr?p0=collectionServiceProvider%20%3C-%20collectionService in file:///C:/workspaces/angular/angular-1.2.1.js (line 78)

Here is the source:

'use strict';

var collectionMockData = {};
var collectionMockErrorData = {};

angular.module('gid.services.collectionMockService', ['ngMockE2E']).run(function($httpBackend, $resource) {
    $httpBackend.whenGET('/api/collection/v1/1234567890').respond(function(method, url, data, headers){
        return [200, collectionMockData, {}];
    });
    $httpBackend.whenGET('/api/collection/v1/1234567891').respond(function(method, url, data, headers){
        return [500, collectionMockErrorData , {}];
    });
});

angular.module('gid.services.collectionService', [ 'ngResource', 'gid.services.collectionMockService' ])
.factory('collectionService', ['$resource', function($resource) {
    var collectionData = null;
    var collectionResource = $resource('/api/collection/v1/:shopperId', { 'shopperId': '@id' });
    return {
        getcollection: function (callback) {
            collectionResource.get({ 'shopperId': '1234567890' }).$promise
            .then(function (data) {
                callback(data);
            }).catch(function (data) {
                callback(data);
            });
        }
    };
}]);

Here is the test:

'use strict';

describe('collectionService', function() {

    beforeEach(function () { 
        angular.module('gid.services.collectionService');
    });


    it('should contain collectionService', inject(function (collectionService) {
        expect(collectionService).not.to.equal(null);

    }));

});

Can someone tell me what am I doing wrong here?

I am mocking the REST API in the source itself, since the backend REST API implementation is not available.

Thanks in advance for your time!

Upvotes: 4

Views: 1412

Answers (1)

v1vendi
v1vendi

Reputation: 613

Instead of

angular.module('gid.services.collectionService');

You should use either

angular.mock.module('gid.services.collectionService');

Or shortand

module('gid.services.collectionService');

Upvotes: 1

Related Questions