【发布时间】:2020-11-25 11:19:08
【问题描述】:
我正在尝试使用 @ngrx/data 为我的 Angular 项目编写单元测试!我收到错误
失败:模块“DynamicTestModule”导入了意外的值“EntityCollectionServiceBase”。请添加@NgModule 注解。
NullInjectorError: R3InjectorError(DynamicTestModule)[CalendarService -> EntityCollectionServiceElementsFactory -> EntityCollectionServiceElementsFactory]: NullInjectorError: 没有提供 EntityCollectionServiceElement
服务
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { Appointment } from '../models/appointment';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from '@ngrx/data';
@Injectable({
providedIn: 'root'
})
export class CalendarService extends EntityCollectionServiceBase<Appointment> {
calendar$: Observable<Appointment>;
constructor(serviceElementsFactory: EntityCollectionServiceElementsFactory) {
super('Appointment', serviceElementsFactory);
}
}
测试文件
import { TestBed, async } from '@angular/core/testing';
import { StoreModule, Store } from '@ngrx/store';
import { CalendarService } from './calendar.service';
import { EntityDataModule } from '@ngrx/data';
import { entityConfig } from '../entity-metadata';
import {
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
} from '@ngrx/data';
describe('CalendarService', () => {
let service: CalendarService;
beforeEach(async(() => {
TestBed.configureTestingModule({
imports: [
StoreModule.forRoot({}),
EntityCollectionServiceBase,
EntityCollectionServiceElementsFactory
],
declarations: [],
providers: [EntityDataModule.forRoot(entityConfig)]
}).compileComponents();
service = TestBed.inject(CalendarService);
}));
it('should be created', () => {
expect(service).toBeTruthy();
});
});
【问题讨论】:
-
将
EntityCollectionServiceBase和EntityCollectionServiceElementsFactory添加到提供者而不是导入中 -
并将
EntityDataModule.forRoot(entityConfig)移动到imports数组中。
标签: angular jasmine ngrx-store ngrx-entity angular-ngrx-data