如何在单元测试中将注入令牌注入组件的构造函数

时间:2018-03-19 17:54:01

标签: angular

我有注射令牌:

export const IS_SEO_PAGE = new InjectionToken<boolean>('accommodation.seo');

我在组件中使用它

constructor(@Inject(IS_SEO_PAGE) private isSeo: boolean,

但是当我尝试将其注入测试时失败并出现错误 Can't resolve all parameters

这是测试:

 beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports: [ TranslateModule.forRoot()],
      declarations: [ StickyNavComponent ],
      providers: [{ provide: IS_SEO_PAGE, useValue: true }],
      schemas: [ NO_ERRORS_SCHEMA ],
    }).compileComponents();
  }));

  it('should create', inject([IS_SEO_PAGE], (isSeo: boolean) => {
    expect(component).toBeTruthy();
  }));

1 个答案:

答案 0 :(得分:0)

关闭InjectionToken的导入顺序非常重要,您希望在所有导入后导入令牌