0

I am trying to write unit tests for my angular project using @ngrx/data!I get the errors

Failed: Unexpected value 'EntityCollectionServiceBase' imported by the module 'DynamicTestModule'. Please add an @NgModule annotation.

NullInjectorError: R3InjectorError(DynamicTestModule)[CalendarService -> EntityCollectionServiceElementsFactory -> EntityCollectionServiceElementsFactory]: NullInjectorError: No provider for EntityCollectionServiceElement

SERVICE

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);
  }
}

TEST FILE



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();
  });
});

1 Answers1

0

Here's my setup

beforeEach(async () => {
    await TestBed.configureTestingModule({
      imports: [
        StoreModule.forRoot({}),
        EffectsModule.forRoot([]),
        EntityDataModule.forRoot({
          entityMetadata
        })
      ],
      providers: [
        MyService,
        { provide: EntityCacheEffects, useValue: {} },
        { provide: EntityDataService, useValue: null }
      ]
    });

    await TestBed.compileComponents();
  });
Jan
  • 320
  • 2
  • 6