簡體   English   中英

嘗試測試通用angularjs $ resource服務時出現未知提供程序錯誤

[英]Unknown Provider Error while trying to test generic angularjs $resource service

我有為我的應用程序創建資源的通用服務:

(function(module) {
module.provider('restService', {

    resourceRegistry: {},
    addRestResource: function(entityName, entityProto) {
        this.resourceRegistry[entityName] = entityProto;
    },

    $get: function() {
        var restService;
        for (var entityName in this.resourceRegistry) {
            createRestResource(entityName, this.resourceRegistry[entityName]);
        };

        restService = {
            //createRestResource: createRestResource
        };

        return restService;
  }});

  function createRestResource(entityName, entityProto) {
    console.log('registering model: ' + entityName);
    module.provider(entityName, { $get: function($resource, $http) {
        var resource = $resource('/api/' + entityName + '/:id', { // TODO use config
            id : '@id' //this binds the ID of the model to the URL param
        },{
            query : { method : 'GET', isArray : true }, //this can also be called index or all
            update : { method : 'PUT' },
            create : { method : 'POST' },
            destroy : { method : 'DELETE' }
        });

        // here gose some other functionality unrelated to the topic...

        return resource;
    }});
}}(angular.module('restService', ['ngResource'])));

我可以被使用的任何其他模塊使用

module.config(['restServiceProvider', function(restServiceProvider) {
  restServiceProvider.addRestResource('User', { name: null, email: null });
}

盡管上面的內容在我的應用程序中實際上對我有用,但實際上上面的內容對我的應用程序都不起作用(由於重構前遺留了一些代碼,它無法正常工作),或者我也無法對其進行茉莉花/業力測試。 問題是嘗試各種方法來配置restServiceProvider我總是以錯誤結尾,例如對於TestEntity ,未知提供程序TestEntityProider 盡管在創建resourceRegistry之前我嘗試了不同的方法來配置resourceRegistry ,但這是一些測試文件。

describe('testing restService', function () {
    var httpBackend;
    var theRestServiceProvider;

    beforeEach(function() {
        module('ngResource');
        module('restService');
        var fakeModule = angular.module('test.app.config', ['ngResource'], function () {});
        fakeModule.config( function (restServiceProvider) {
            theRestServiceProvider = restServiceProvider;
            restServiceProvider.addRestResource('TestModel', {testProp: null});
        });

        module('test.app.config');
    });

    beforeEach(function () {
        inject(function ($httpBackend) {
            httpBackend = $httpBackend;
        })
    });

    beforeEach(inject(function (restService) {}));



    describe('create restService entity', function() {
        it('should post entity with nonempty testProp',
            function() {
                theRestServiceProvider.addRestResource('TestModel', {testProp: null});

                inject(function(TestModel) {
                    var object = new TestModel();
                    object.testProp = 'John Doe';

                    httpBackend.expectPOST(/.*/,
                        function(postData) {
                            console.log("post data: " + postData);
                            jsonData = JSON.parse(postData);
                            expect(jsonData.testProp).toBe(object.testProp);

                            return jsonData;
                        }).respond({});

                    var response = object.$create();

                    httpBackend.flush();
                });
            });
    });
});

IMO這是因為在測試中,注冊資源“太遲了”,但是我仍然不知道如何正確執行此操作。

在這里編輯是最終分辨率的快捷方式

(function(module) {
  module.provider('restService', function($provide) {
    var provider = {};
    // all the provider stuff goes here

    function createRestResource(entityName, entityProto) {
      /* 
       * using $provider here is fundamental here to properly create 
       * 'entityName' + Provider in the runtime instead of module initialisation
       * block
       */
       $provide.factory(entityName, function($resource, $http) { /* ... */ };
       // do other stuff...
    }

    return provider;
  }
}(angular.module('restServiceModule', ['ngResource'])))

我在這里做了非常相似的事情https://github.com/tunguski/matsuo-ng-resource/blob/master/matsuo-ng-resource.js ,也許它將對您有所幫助。

基本上,我在$provide添加了新的資源提供者,而不是module 有用。 我認為這是主要區別。

我對業力/茉莉花不是很熟悉,但是BDD語法似乎很相似。 據我了解,你應該有這樣的東西

beforeEach(function () {
    module('restService');
    inject(function (_$httpBackend_, _restService_, _ngResource_) {
        $httpBackend = _$httpBackend_;
        restService = _restService_;
        ngResource = _ngResource_;
    })
});

而不是你beforeEach 在這里閱讀更多。 如果要提供模擬而不是注入的服務,則可以使用

    module('restService', function($provide){
        $provide.value('serviceToMock', mockObject);
    });

請注意,在以后的階段中,使用相同名稱命名模塊和提供者/服務/工廠可能會造成混淆...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM