Angular 2 RC5单元测试 - ComponentFixture detectChanges()无效

时间:2016-09-02 15:52:26

标签: unit-testing angular

我遇到了使用Angular 2 RC5的单元测试问题,但似乎没有出现ComponentFixture的detectChanges方法正如我期望的那样工作。以下是我的代码片段:

  describe('Component Test 1', () => {
    beforeEach(async(() => {
      TestBed.compileComponents();
    }));

    it('should calculate base price', async(() => {
      var fixture = TestBed.createComponent(CalcComponent);
      console.log('Got here 1.');
      fixture.detectChanges();
      console.log('Got here 2.');

      expect(fixture.componentInstance.calculateBasePrice()).toBeGreaterThan(50);
    }));
  });

基本上,当运行此单元测试时,'到达此处1。'打印但是测试永远不会继续打印第二个'到达这里2。'但是,我没有得到任何有关错误信息的有用错误消息。我得到的只是一个非常通用的消息,告诉我测试失败了,尽管它显然从来没有达到期望线,因为第二个console.log行从未打印过:

SUMMARY:
√ 9 tests completed
× 1 test failed

FAILED TESTS:
  CalcComponent Tests
    Component Test 1
      × should calculate base price
        PhantomJS 2.1.1 (Windows 7 0.0.0)
      detectChanges
      detectViewChildrenChanges
      detectChangesInternal
      detectChanges
      detectChanges
      detectViewChildrenChanges
      detectChangesInternal
      detectChanges
      detectChanges
      detectChanges
...

我知道问题出在fixture.detectChanges()行周围,因为如果我对它进行注释,则会打印第二个console.log,并且由于未定义的对象(可能是因为fixture),我得到关于期望失败的错误。从未调用detectChanges())。有没有人知道这里会发生什么?

0 个答案:

没有答案