【问题标题】:Unexpected value 'StateService' declared by the module 'DynamicTestModule'模块“DynamicTestModule”声明的意外值“StateService”
【发布时间】:2017-11-14 15:33:56
【问题描述】:

我在 Angular 5 项目中使用 UI 路由器。为页脚组件运行测试时,出现此错误:

Failed: Unexpected value 'StateService' declared by the module 'DynamicTestModule'. Please add a @Pipe/@Directive/@Component annotation.

但是,我正在导入 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();
  });
});

知道我哪里出错了吗?

【问题讨论】:

    标签: angular unit-testing karma-runner


    【解决方案1】:

    declarations 数组仅适用于可声明的类:组件、指令和管道。像这样将 StateService 添加到 providers 数组中:

    TestBed.configureTestingModule({
      imports: [],
      declarations: [ FooterComponent ],
      providers: [StateService]
    })
    

    【讨论】:

    • 在我的单元测试中,我使用了 ngx-cookie-service npm 包,但我忘记将它作为 CookieService 包含在提供程序下。感谢您的解决方案!
    猜你喜欢
    • 2017-06-10
    • 2017-12-24
    • 2017-06-03
    • 2021-06-19
    • 1970-01-01
    • 1970-01-01
    • 2020-02-09
    • 2016-12-24
    • 1970-01-01
    相关资源
    最近更新 更多