unit-testing - 使用 fakeAsync 进行 Angular2 测试

标签 unit-testing angular karma-jasmine

我正在尝试使用 fakeAsync 来测试 Angular 2 组件,但未设置固定装置变量。事实上,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();
})));

当我运行此代码时,我得到:

Failed: Cannot read property 'detectChanges' of undefined TypeError: Cannot read property 'detectChanges' of undefined

我不明白为什么回调没有被触发。在此存储库中,它工作正常:https://github.com/juliemr/ng2-test-seed/blob/master/src/test/greeting-component_test.ts

有什么线索吗?

注意:我使用的是 ES6、Traceur、Angular 2 beta、Karma 和 Jasmine。

------更新------

它遵循测试失败的存储库:

https://github.com/cangosta/ng2_testing_fakeasync

最佳答案

TestComonentBuilder 不适用于 templateUrl https://github.com/angular/angular/issues/5662

关于unit-testing - 使用 fakeAsync 进行 Angular2 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34741775/

相关文章:

unit-testing - Resharper 10 : Unit test output window is broken

angular - 为什么我在刚启动的 Angular2 应用程序中会收到此错误?

javascript - Karma - 如何指向 typescript 覆盖的源 map

react 形式的 Angular Testing 提交事件

android - dexDebugTest 构建任务中出现 "The command line is too long"错误

unit-testing - Vue JS - 单元测试 - 未定义本地存储

unit-testing - 如何在服务结构中对无状态服务进行单元测试

javascript - 由于过滤器导致表达式更改而导致 *ngIf 的渲染延迟 - Angular

typescript - 为什么我必须调用 NgZone.run 让我的 View 在 Angular2 中使用 breezejs 进行更新?

javascript - 虽然 Karma 在本地通过,但 Travis CI 上的 Firefox 语法错误