2016-12-01 69 views
3

我正在測試角度cli項目中的很多組件,並且我在其中一些組件中使用RouterTestingModule來存根路由器。我只想將RouterTestingModule添加到所有測試中,因此我不必選擇添加它。與全球提供商進行角度2測試

我將它添加到test.js中的測試設置,如下所示,但它似乎並未包含在組件的測試模塊中。這是否包括「全球」提供者的正確方法?

Promise.all([ 
    System.import('@angular/core/testing'), 
    System.import('@angular/platform-browser-dynamic/testing'), 
    System.import('@angular/router/testing'), 
]) 
    // First, initialize the Angular testing environment. 
    .then(([testing, testingBrowser, testingRouter]) => { 
     testing.getTestBed().initTestEnvironment(
      testingBrowser.BrowserDynamicTestingModule, 
      testingBrowser.platformBrowserDynamicTesting(), 
      testingRouter.RouterTestingModule, 
     ); 
    }) 
    // Then we find all the tests. 
    .then(() => require.context('./', true, /\.spec\.ts/)) 
    // And load the modules. 
    .then(context => context.keys().map(context)) 
    // Finally, start Karma to run the tests. 
    .then(__karma__.start, __karma__.error); 

文檔說這個約initTestEnvironment

這可能只會被調用一次,建立共同的供應商爲 當前測試套件在當前平臺上。

回答

0

稍晚但解決方法是將一組提供者傳遞給platformBrowserDynamicTesting()函數。

Promise.all([ 
 
    System.import('@angular/core/testing'), 
 
    System.import('@angular/platform-browser-dynamic/testing'), 
 
    System.import('@angular/router/testing'), 
 
]) 
 
    // First, initialize the Angular testing environment. 
 
    .then(([testing, testingBrowser, testingRouter]) => { 
 
     testing.getTestBed().initTestEnvironment(
 
      testingBrowser.BrowserDynamicTestingModule, 
 
      testingBrowser.platformBrowserDynamicTesting([..<providers here>..]) 
 
     ); 
 
    }) 
 
    // Then we find all the tests. 
 
    .then(() => require.context('./', true, /\.spec\.ts/)) 
 
    // And load the modules. 
 
    .then(context => context.keys().map(context)) 
 
    // Finally, start Karma to run the tests. 
 
    .then(__karma__.start, __karma__.error);