Angular2-运行所有单元测试会使个别通过的某些测试失败

时间:2019-11-21 12:59:39

标签: angular unit-testing karma-jasmine

我正在为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的新手,所以如果需要提供信息或代码,请告诉我。

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

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

afterEach {
    fixture.destroy();
}

有关更多详细信息,请检查-> https://github.com/angular/angular/issues/18831

相关问题