【发布时间】:2016-10-03 15:57:44
【问题描述】:
我正在尝试了解 Angular2 测试 API 的基础知识,而 TestBed.compileComponents() 让我抓狂。要么我这样称呼它:
beforeEach( done => {
TestBed.configureTestingModule({
declarations: [MyComponent]
})
.compileComponents().then( () => {
fixture = TestBed.createComponent(MyComponent);
component = fixture.componentInstance();
});
done();
});
然后我的组件在我的测试中未定义(我相信因为 compileComponent 是异步的,所以在我的 var 组件获取值之前运行测试)
要么这样(如documentation 中所述):
beforeEach( async(() => {
TestBed.configureTestingModule({
declarations: [MyComponent]
}))
.compileComponents();
beforeEach( () => {
fixture = TestBed.createComponent(HomeComponent);
component = fixture.componentInstance();
});
然后我得到错误:This test module uses the component HomeComponent which is using a "templateUrl", but they were never compiled. Please call "TestBed.compileComponents" before your test.
有人可以帮忙吗?
忘了说我使用 webpack 和 RC6
【问题讨论】:
-
如果在 compileComponents 编译组件时调用 done() 会发生什么,即在传递给 then() 的函数内部?
-
@JBNizet 同样的事情,我得到一个未定义的组件。顺便说一句,很棒的 angular2 书
-
你能展示你的组件的代码和模板吗?
标签: unit-testing angular karma-jasmine