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

问题描述 投票:0回答:1

我们为具有许多嵌套服务和组件的组件编写测试用例(我们不能根据我们的要求模拟每个服务)。我们创建了包含所有组件和服务的模块。在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();
  });
unit-testing jasmine angular2-testing
1个回答
0
投票

添加afterEach有帮助吗?

afterEach {
    fixture.destroy();
}

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

© www.soinside.com 2019 - 2024. All rights reserved.