加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 17:42:06 所属栏目:安全 来源:网络整理
导读:我试图获得Angular2测试API和TestBed.compileComponents()的基础知识让我疯狂.我要么像这样称呼它: beforeEach( done = { TestBed.configureTestingModule({ declarations: [MyComponent] }) .compileComponents().then( () = { fixture = TestBed.createCo
我试图获得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();
    });

你不需要这里的异步性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读