我正在尝试使用@ngrx/data 为我的角度项目编写单元测试!我得到了错误
失败:模块“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();
});
});