2016-12-01 9 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);