【问题标题】:What is the recommended approach to configure TestBed in Jasmine test cases in Angular?在 Angular 的 Jasmine 测试用例中配置 TestBed 的推荐方法是什么?
【发布时间】:2021-01-04 09:25:59
【问题描述】:

我已经开始为 Angular 编写测试用例,在阅读互联网上的文章时,我发现有不同的方式可以配置我们的 TestBed。下面是几个例子:

示例 1:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents()
    .then(() => {
      fixture = TestBed.createComponent(Component);
      component = fixture.componentInstance;
      fixture.detectChanges();
    });
}));

示例 2:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents();
}));
beforeEach(() => {
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

示例 3:

beforeEach(async(() => {
  TestBed.configureTestingModule({
   ...
  }).compileComponents();
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
}));

示例 4:

beforeEach(async() => {
  TestBed.configureTestingModule({
   ...
  });
  await TestBed.compileComponents();
  fixture = TestBed.createComponent(Component);
  component = fixture.componentInstance;
  fixture.detectChanges();
});

我需要帮助了解推荐的方法是什么?

【问题讨论】:

    标签: angular jasmine testbed


    【解决方案1】:

    如果它们都有效,您可以选择自己喜欢的。我个人使用示例 2,因为我认为大多数地方都是这样做的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-01
      • 2015-06-17
      • 2018-02-17
      • 1970-01-01
      • 2011-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多