在beforeAll中创建一次TestBed之后,无法在beforeEach中重置提供程序

时间:2018-01-05 10:34:11

标签: unit-testing jasmine angular2-testing

我们为具有许多嵌套服务和组件的组件编写测试用例(我们不能根据我们的要求模拟每个服务)。我们创建了包含所有组件和服务的模块。在spec.ts文件中,我们在TestBed中添加了模块,因为beforeEach需要花费大量时间来创建环境。 因此我们将beforeEach替换为beforeAll,但是当我们在测试用例中更改某些内容时,相同的更改会传递给下一个测试用例。

beforeAll(async(() => {
    TestBed.configureTestingModule({
      imports: [SharedModule]
    })
    .compileComponents();
    fixture = TestBed.createComponent(ChildComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  }));

  it('name should xyz', () => {
    component.name = 'xyz';
    expect(component.name).toEqual('xyz')
  });

    it('some other test case', () => {
      //I want component.name to be reset to default value
    expect(component.prop1).toBeTruthy();
  });

1 个答案:

答案 0 :(得分:0)

添加afterEach帮助吗?

afterEach {
    fixture.destroy();
}

有关设置/拆卸功能的Jasmine文档位于:https://jasmine.github.io/2.1/introduction#section-Setup_and_Teardown

相关问题