Unit testing 角度2 RC5单元测试-组件夹具检测更改()不工作

Unit testing 角度2 RC5单元测试-组件夹具检测更改()不工作,unit-testing,angular,Unit Testing,Angular,我遇到了Angular 2 RC5的一个单元测试问题,在这个问题上,ComponentFixture detectChanges方法似乎没有按照我预期的方式工作。以下是我的代码片段: describe('Component Test 1', () => { beforeEach(async(() => { TestBed.compileComponents(); })); it('should calculate base price', a

我遇到了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);
    }));
  });
基本上,当这个单元测试运行时,“Got here 1.”会被打印出来,但测试永远不会继续打印第二行“Got here 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()从未被调用),我会得到一个关于预期失败的错误。有人知道这里可能发生了什么吗?

我认为问题在于fixture.detectChanges()其中应使用ComponentFixture.whenStable。我知道TestBed.compileComponents()是异步的,并返回一个承诺,因此detectChnages可能存在一些时间问题。从理论上讲,该名称建议ComponentFixture.whenStable应能解决此问题。仍在研究;)请看,我认为问题与fixture.detectChanges()有关,其中应使用as ComponentFixture.whenStable。我知道TestBed.compileComponents()是异步的,并返回一个承诺,因此detectChnages可能存在一些时间问题。从理论上讲,该名称建议ComponentFixture.whenStable应能解决此问题。仍在研究;)看看