2014-12-06 32 views
0

我無法讓我的測試運行,因爲依賴項沒有被正確注入。

我得到的錯誤是在標題中定義的。我已經包含了我的解決方案中的實際測試代碼,app.js & index.html文件。

問題在於延遲引導,我不是很陌生,因爲它被我的一個同事包括在內。如果我從app.js文件中刪除「的app.config(函數(STARTUP_CONFIG ...」,然後在測試運行正常

我怎樣才能正確地注入STARTUP_CONFIG在我的測試?

測試代碼

.. 
.. 
describe("test description...", function() { 

var app; 
var mockupDataFactory; 

beforeEach(module('Konstrukt')); 

beforeEach(inject(function (STARTUP_CONFIG,BUDGETS,APPLICATIONS) { 
    //failed attempt to inject STARTUP_CONFIG 
})); 

beforeEach(function() { 
    app = angular.module("Konstrukt"); 
}); 

beforeEach(function() { 
    mockupDataFactory = konstruktMockupData.getInstance(); 
}); 

it('should be accessible in app module', function() { 
    expect(app.pivotTableService).toNotBe(null); //this test runs fine 
}); 

it('test decr...', inject(function (pivotTableService) { 
    ... //Fails here 
.. 
.. 

app.js

.. 
    ..   
    angular.module('Konstrukt', ['ngGrid', 'ngSanitize', 'ngRoute','pasvaz.bindonce', 'ngAnimate', 'nvd3ChartDirectives', 'ui.select', 'ngProgress', 'ui.grid', 'ui.grid.edit','ui.grid.selection', 'ui.grid.cellNav', 'ui.grid.pinning', 'ui.grid.resizeColumns']); 

      var app = angular.module('Konstrukt'); 
      app.config(function (STARTUP_CONFIG, BUDGETS, APPLICATIONS) { 

     var STARTUP_CONFIG = STARTUP_CONFIG; 
     var BUDGETS = BUDGETS; 
     var APPLICATIONS = APPLICATIONS; 

     }); 
     .. 
     .. 

index.html的

.. 
    .. 

    <script> 
    setTimeout(function(){ 
    window.deferredBootstrapper.bootstrap({ 
    element: window.document.body, 
    module: 'Konstrukt', 
    resolve: { 
     STARTUP_CONFIG: ['$http', function ($http) { 
     return $http.get('/scripts/_JSON/activeBudgets.JSON'); 
     }], 
     BUDGETS: ['$http', function ($http) { 
     return $http.get('/scripts/_JSON/activeBudgets.JSON'); 
     }], 
     APPLICATIONS: ['$http', function ($http) { 
     return $http.get('/scripts/_JSON/applications.JSON'); 
     }] 
    } 
    }) 
    } , 1500); 
</script> 

回答

1

deferredBootstrapper不會在單元測試中運行,這意味着它通常添加到您的模塊將不可用的常數。

您可以添加一個全球beforeEach是提供嘲笑他們的版本:

beforeEach(function() { 
    module(function ($provide) { 
    $provide.constant('STARTUP_CONFIG', { something: 'something' }); 
    $provide.constant('BUDGETS', { something: 'something' }); 
    $provide.constant('APPLICATIONS', { something: 'something' }); 
    }); 
}); 
+0

該訣竅,謝謝。由於我在測試中並沒有使用這些常量(顯然無法使用這些常量),所以它開箱即用:=) – JohanLarsson 2014-12-07 08:42:01

+0

不客氣:) – tasseKATT 2014-12-07 10:30:32

相關問題