【问题标题】:Angular 8 testing error Unexpected value 'DecoratorFactory' imported by the module 'DynamicTestModule'Angular 8 测试错误模块“DynamicTestModule”导入的意外值“DecoratorFactory”
【发布时间】:2020-11-23 17:16:55
【问题描述】:

我正在尝试将 Jasmine & Karma 框架制作为在 8.2 版中运行的当前 Angular 应用程序。但是我在 Karma 测试运行窗口中遇到了这个奇怪的错误:

失败:模块“DynamicTestModule”导入了意外的值“DecoratorFactory”。请添加@NgModule 注解。

有什么问题?

我的 componenent.spec.ts 看起来像这样:

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { HomeComponent } from './home.component';
import { NO_ERRORS_SCHEMA} from '@angular/core';
import {RouterTestingModule} from '@angular/router/testing';
import {HttpClientTestingModule} from '@angular/common/http/testing';
import { MsalService } from '@azure/msal-angular';
import { Store } from '@ngrx/store';
import { Pipe } from '@angular/core';


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

  beforeEach(async(() => {
    TestBed.configureTestingModule({
       imports: [RouterTestingModule, HttpClientTestingModule, Pipe]
      ,declarations: [HomeComponent]
      ,schemas:[NO_ERRORS_SCHEMA]
      ,providers: [
        {provide: MsalService, useFactory: '' },
        {provide: Store, useFactory: '' }
      ]
    })
    .compileComponents();
  }));

    it('should have header text', async(() => {
      const fixture = TestBed.createComponent(HomeComponent);
      fixture.detectChanges();
    
      const compiled = fixture.debugElement.nativeElement;
      //expect(compiled.querySelector('.header-txt').textContent).toContain('Tax');    
  }));
});

【问题讨论】:

  • 请提出我在哪里犯了错误?
  • 你能从imports 中删除pipe 并告诉我

标签: karma-jasmine angular8 karma-runner


【解决方案1】:

我找到了原因:-

export class MockStore<T> {
      private state: BehaviorSubject<T> = new BehaviorSubject(undefined);
      
      setState(data: T) { this.state.next(data); }

      select(selector?: any): Observable<T> {
        return this.state.asObservable();
      }

      pipe() {}
      dispatch(action: any) { }
}

================================================ ==========================

 TestBed.configureTestingModule({
    {provide: Store, useFactory: 'MockStore' }
    ..............

useFactory 属性必须是一些自定义类名。现在我嘲笑了 store 类。

【讨论】:

    猜你喜欢
    • 2017-05-07
    • 2018-05-24
    • 2017-12-11
    • 2017-06-03
    • 2021-06-19
    • 2017-02-22
    • 1970-01-01
    • 1970-01-01
    • 2017-06-10
    相关资源
    最近更新 更多