2016-05-04 161 views
0

我努力帶來了一個角項目的單元測試的水平,但努力以測試AngularJS工廠有依賴關係。測試角度工廠

我廠擁有5個依存關係,但我甚至無法得到測試的工作,只是測試,工廠被定義。以下是我正在做的一些示例代碼。我一直在使用注射嘗試,$提供ECT,但我不斷收到錯誤沿

"Configuration parameter should be an object, instead it is a: undefined" 

這裏的線路試驗,到目前爲止,我一直留在註釋掉的代碼顯示了我一直在努力:

(function() { 

'use strict'; 

describe('While working with the channelService', function(){ 

var sut, 
    q, 
    filter, 
    common, 
    dataContext, 
    etCommon; 

beforeEach(function(){ 

    // Load the module 
    module('app'); 
    module('app.inventory'); 
    module('blocks.router'); 

    inject(function($injector){ 
    sut = $injector.get('channelService'); 
    q = $injector.get('$q'); 
    filter = $injector.get('$filter'); 
    common = $injector.get('common'); 
    dataContext = $injector.get('dataContext'); 
    etCommon = $injector.get('etCommons'); 

    spyOn(q, 'defer'); 
    spyOn(filter , 'defer'); 
    spyOn(common, 'makeStruct'); 
    spyOn(q, 'defer'); 
    spyOn(q, 'defer'); 
    }); 




describe('should define the following publicly', function(){ 
    it('should be defined itself', function(){ 
     expect(sut).toBeDefined(); 
    }); 
}); 

});

})();

服務是沿

(function() { 

angular.module('app.inventory').factory('channelService', [ 
    '$q', '$filter', 'common', 'dataContext', 'etCommon', 
    function ($q, $filter, common, dataContext, etCommon) { 
... 

... 
]); 

}()); 
+0

郵政服務定義,從你的問題刪除所有註釋掉的代碼,並張貼的完整,準確錯誤信息。 –

+0

在你的測試中,你從注入器獲得'etCommons'。但是該服務名爲'etCommon'。順便說一句,爲什麼使用注入器,而不是直接在測試中注入服務? '注入(函數(channelService,$ Q,$過濾器,...){' –

+0

如果我只注入實際的服務,我應該至少能指望它雖然我不應該定義?沒有嘲諷一切依賴 beforeEach(注入(函數(channelService){ SUT = channelService; })); –

回答

0

線嘗試使用ngMock在beforeEach

https://docs.angularjs.org/api/ngMock