我正在尝试使用fakeAsync来测试Angular 2组件,但是没有设置fixture变量 . 事实上,没有调用promise回调 . 这是代码:
@Component({
template: '',
directives: [GroupBox, GroupBoxHeader]
})
class TestComponent {
expandedCallback() { this.expandedCalled = true; }
}
it('testing...', inject([TestComponentBuilder], fakeAsync((tcb) => {
var fixture;
tcb.createAsync(TestComponent).then((rootFixture) => {
fixture = rootFixture
});
tick();
fixture.detectChanges();
})));
当我运行此代码时,我得到:
失败:无法读取未定义TypeError的属性'detectChanges':无法读取未定义的属性'detectChanges'
我可以__87436_解雇了 . 在这个存储库中,它工作正常:https://github.com/juliemr/ng2-test-seed/blob/master/src/test/greeting-component_test.ts
任何线索?
注意:我使用的是ES6,Traceur,Angular 2 beta,Karma和Jasmine .
------更新------
它遵循失败测试的存储库:
2 回答
试试这种方式https://github.com/antonybudianto/angular2-starter/blob/master/app/simplebind/child.component.spec.ts#L15
重点是您创建一个测试虚拟组件(例如
TestComponent
)并在directives: [...]
中注册要测试的组件并使用template: <my-cmp></my-cmp>
,然后将TestComponent
传递给tsb.createAsync(TestComponent)...
,并使用injectAsync
.我更喜欢这种方式,因为我可以轻松地模拟来自父级的数据,并传递任何输入并处理组件的输出 .
TestComonentBuilder
不适用于templateUrl
https://github.com/angular/angular/issues/5662