unit-testing - Angular 2.0.0 - 测试 "imported by the module ' DynamicTestModule' "

标签 unit-testing angular karma-jasmine

我在 Angular 2 中测试 app.component.ts 时遇到问题。我正在使用 angular-cli。每当我运行 ng test 时,我的 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 ]
});

谁能帮我解决这个问题?

最佳答案

HomeModuleComponentComponent 而不是 Module,所以它必须在声明中:

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

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

TestBed.createComponent(AppComponent);

关于unit-testing - Angular 2.0.0 - 测试 "imported by the module ' DynamicTestModule' ",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40014712/

相关文章:

reactjs - React 测试无法解析动态需求路径

javascript - Angular 4测试错误

flutter - 在Flutter和Dart中如何测试数据是否成功写入json?

javascript - 水平居中 MatDialog,宽度为 100%,最大宽度固定

Angular 2 : Http Response Status Codes as constants

javascript - Ng 日期时间选择器的值更改不起作用

angular - 单击事件发生时如何对下拉菜单进行单元测试

javascript - Sequelize createAt/updatedAt 时间戳(毫秒)四舍五入为 000?

java - 如何使用 JUnit 测试特定于操作系统的方法?

java - 如何在单元测试执行之前实例化 session ,以便服务类中的 Autowiring session 正确创建 bean?