模块“DynamicTestModule”声明了意外值“StateService”

aby*_*e85 3 unit-testing karma-runner angular

我在 Angular 5 项目中使用 UI Router。当运行页脚组件的测试时,我收到此错误:

Failed: Unexpected value 'StateService' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.
Run Code Online (Sandbox Code Playgroud)

但是,我正在导入 StateService 并将其包含在 TestBed 的声明数组中。

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { FooterComponent } from './footer.component';
import { StateService } from '@uirouter/angular';

fdescribe('FooterComponent', () => {
  let component: FooterComponent;
  let fixture: ComponentFixture<FooterComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ StateService ],
      declarations: [ FooterComponent ],
    })
    .compileComponents();
  }));

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

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});
Run Code Online (Sandbox Code Playgroud)

知道我哪里出错了吗?

Fab*_*üng 5

声明数组仅适用于可声明的类:组件、指令和管道。将StateService添加到提供者数组中,如下所示:

TestBed.configureTestingModule({
  imports: [],
  declarations: [ FooterComponent ],
  providers: [StateService]
})
Run Code Online (Sandbox Code Playgroud)