使用jasmine测试角度工厂时出现未知的提供程序错误

时间:2013-11-22 15:47:58

标签: angularjs jasmine

我正在尝试使用jasmine测试角度“服务”(实际上是使用工厂实现),并收到以下错误:

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)

以下是来源:

'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);
            });
        }
    };
}]);

以下是测试:

'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);

    }));

});

有人可以告诉我这里我做错了什么吗?

我在源代码中嘲笑REST API,因为后端REST API实现不可用。

提前感谢您的时间!

1 个答案:

答案 0 :(得分:1)

而不是

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

你应该使用

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

或简短

module('gid.services.collectionService');