单元测试 – Angular2:无法使用TestBed.compileComponent()使用templateUrl测试组件

前端之家收集整理的这篇文章主要介绍了单元测试 – Angular2:无法使用TestBed.compileComponent()使用templateUrl测试组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图获得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();
});

但后来我得到错误:这个测试模块使用组件HomeComponent,它使用“templateUrl”,但它们从未编译过.请在测试前调用“TestBed.compileComponents”.

有人可以帮忙吗?

忘了说我使用webpack和RC6

解决方法

试试这个:

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

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

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

你不需要这里的异步性.

猜你在找的Angularjs相关文章