简体   繁体   中英

Unknown provider when testing factory

I'm getting the following error when trying to test a factory in angualar:

Error: [$injector:unpr] Unknown provider: $resourceProvider <- $resource <- myService

There are already tests in the project that do similar things and they work fine, so I can't for the life of me figure out why these don't work. Here is the impl code:

(function () {
'use strict';

var module = angular.module('first.module', []);

function firstThing() {
    this.doSomething = function (something) {
        return campaign;
    };
}

module.service('firstThing', firstThing);

function myServiceFactory($resource, notifier) {
    var Resource = $resource('/api/campaigns/:id', { id: '@id' }, {
        get: { method: 'GET' }
    });

    function listItems() {
        return [];
    }

    return {
        list: listItems
    };
}

module.factory('myService', myServiceFactory);

})();

The test code is:

'use strict';

describe('My service test', function () {

    var myServiceFactory, campaign, $injector;

    beforeEach(module('first.module'));

    beforeEach(inject(function (_$injector_) {
        $injector = _$injector_;
        myServiceFactory = $injector.get('myService');
    }));

    it('true is true', function () {
        expect(true).toEqual(true);
    });
});

Karma is being used for the tests. Thanks for any help.

您应该将ngResource模块添加到模块依赖项中。

var module = angular.module('first.module', ['ngResource']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM