与全球供应商进行Angular 2测试
我正在angular cli项目中测试许多组件,并且在其中一些组件中使用与全球供应商进行Angular 2测试,angular,karma-runner,angular-cli,Angular,Karma Runner,Angular Cli,我正在angular cli项目中测试许多组件,并且在其中一些组件中使用RouterTestingModule来存根路由器。我只想将RouterTestingModule添加到所有测试中,这样就不必选择性地添加它 我将它添加到test.js中的测试设置中,如下所示,但它似乎没有包含在组件的测试模块中。这是包含“全球”提供商的正确方式吗 Promise.all([ System.import('@angular/core/testing'), System.import('@ang
RouterTestingModule
来存根路由器。我只想将RouterTestingModule
添加到所有测试中,这样就不必选择性地添加它
我将它添加到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
:
这只能调用一次,以便为
当前平台上的当前测试套件
虽然有点晚,但解决方案是将一个提供程序数组传递给platformBrowserDynamicTesting()函数
Promise.all([
系统导入(“@angular/core/testing”),
System.import(“@angular/platform browser dynamic/testing”),
System.import(“@angular/router/testing”),
])
//首先,初始化角度测试环境。
.然后(([测试,测试浏览器,测试路由器])=>{
testing.getTestBed().initTestEnvironment(
testingBrowser.Browser动态测试模块,
testingBrowser.PlatformBrowser动态测试([…])
);
})
//然后我们找到所有的测试。
.then(()=>require.context('./',true,/\.spec\.ts/))
//并加载模块。
.then(context=>context.keys().map(context))
//最后,启动Karma来运行测试。
.然后(uuuu业力开始,uuu业力错误)代码>