Angular 2.0.0 - 测试“由模块导入'DynamicTestModule'”

时间:2016-10-13 07:22:54

标签: unit-testing angular karma-jasmine

我在测试Angular 2中的app.component.ts时遇到问题。我正在使用angular-cli。每当我运行测试时,我的app.component.spec.ts会使控制台提示错误:

 Failed: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'
 Error: Unexpected directive 'HomeModuleComponent' imported by the module 'DynamicTestModule'

我在TestBed中导入了HomeModuleComponent

TestBed.configureTestingModule({
  declarations: [AppComponent],
  imports : [ HomeModuleComponent ]
});

任何人都可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:22)

// make sure that it's the correct user if(user_id.equals(userid)) { // check if password was changed successfully if(cur_pass.equals(pass)) { // successful password change } else { // something went wrong with the password change } } else { // this else is just to help you see your mistake // in your code you raised the error here! } HomeModuleComponent不是Component,因此必须在声明中:

Module

然后您可以创建要测试的组件,

TestBed.configureTestingModule({
  declarations: [AppComponent, HomeModuleComponent],
  imports : [ ]
});

答案 1 :(得分:2)

在我的测试规范中,我错误地导入了service而不是提供它。我得到了同样的错误。

service数组中添加providers解决了我的错误。