我在测试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 ]
});
任何人都可以帮我解决这个问题吗?
答案 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
解决了我的错误。