我正在尝试获得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

最佳答案

试试这个:

describe('FooComponent', function () {
    let fixture: ComponentFixture<FooComponent>;

    beforeEach(() => {
        TestBed.configureTestingModule({
            declarations: [FooComponent]
        });

        fixture = TestBed.createComponent(FooComponent);
        fixture.detectChanges();
    });

您在这里不需要异步。

10-06 11:48