Angular 6.如何测试注入的组件

时间:2018-11-21 12:18:30

标签: javascript angular testing angular-test

我有一个FAB组件,该组件分为三个部分:触发组件,操作组件和主要组件,它们全部聚集在一起。

Trigger和Actions组件具有注入的父级,如下所示:

constructor(
  @Inject(forwardRef(() => FabSpeedDialComponent)) private _parent: 
FabSpeedDialComponent,
  private renderer: Renderer
) { }

父母对其行动有循环依赖性:@ContentChild(FabSpeedDialActionsComponent) _childActions: FabSpeedDialActionsComponent;

效果很好,但是我在测试此行为时遇到问题。例如,对actions组件进行了测试:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { FabSpeedDialActionsComponent } from './fab-speed-dial-actions.component';

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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [FabSpeedDialActionsComponent]
    })
      .compileComponents();
  }));

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

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

由于以下错误而失败:

  

错误:   StaticInjectorError(DynamicTestModule)[FabSpeedDialActionsComponent->   FabSpeedDialComponent]:StaticInjectorError(平台:   核心)[FabSpeedDialActionsComponent-> FabSpeedDialComponent]:       NullInjectorError:没有FabSpeedDialComponent的提供程序!

即使我尝试将其添加到声明中,它仍然会失败。

0 个答案:

没有答案