Angular2 - 运行 所有单元测试均未通过某些单独通过的测试

Angular2 - Running all Unit Tests Fails Some Tests that Pass Individually

我正在为我的 Angular 项目 (Jasmine Karma) 编写单元测试。

所有测试都单独通过,但如果我运行整套测试,一个测试失败并出现以下异常:

Uncaught TypeError: _this.handler.handle is not a function thrown

跳过失败测试之前的测试,所有测试再次通过。所以我猜我需要在每次或某些测试后进行清理。

这是正确的吗?如果是这样,我该如何进行测试的实际清洁。我似乎在网上找不到任何东西。

测试失败前的测试

    describe('CreateBindingComponent', () => {
      let component: CreateBindingComponent;
      let fixture: ComponentFixture<CreateBindingComponent>;

      beforeEach(async(() => {
        TestBed.configureTestingModule({
          declarations: [ CreateBindingComponent ],
          providers:[
            GetBindingEnumsService,
            GetBindingService,
            HttpClient,
            HttpHandler,
            CreateService,
          ],
          imports: [FormsModule, SelectDropDownModule, RouterTestingModule, MaterialModule]
        })
        .compileComponents();
      }));

      beforeEach(() => {
        fixture = TestBed.createComponent(CreateBindingComponent);
        component = fixture.componentInstance;
        fixture.detectChanges();
      });

      it('should create', () => {
        expect(component).toBeTruthy();
      });
    });

未通过测试

describe('DeleteDialogComponent', () => {
  let component: DeleteDialogComponent;
  let fixture: ComponentFixture<DeleteDialogComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [ DeleteDialogComponent ],
      providers: [
        {provide : MatDialogRef, useValue : {}},
        {provide: MAT_DIALOG_DATA, useValue: {}},
        DeleteBindingService,
        HttpClient,
        HttpHandler
      ],
      imports: [MaterialModule]
    })
    .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(DeleteDialogComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

我是 Angular 的新手,所以如果我需要提供信息或代码,请告诉我。

在此先感谢您的帮助。

我认为 Angular 不会自动销毁侦听器以帮助您获得有关测试执行的更多详细信息。要删除它,您只需使用 afterEach.

afterEach {
    fixture.destroy();
}

更多详情请查看 -> https://github.com/angular/angular/issues/18831