Invalid provider for DynamicTestModule NgModule when testing a service in Angular 2

I have the following service:

import { Injectable } from '@angular/core';

import { MenuItem } from './../classes/menu-item';
import { ITEMS } from './../static-data/items-list';

@Injectable()
export class ItemsListService {

    getItems(): Promise<MenuItem[]> {
        return Promise.resolve(ITEMS);
    }

}

The test for this service is here:

import { TestBed, async, inject } from '@angular/core/testing';

import { ItemListService } from './item-list.service';
import { MenuItem } from './../classes/menu-item';
import { ITEMS } from './../static-data/items-list';

describe('ItemListService', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
        providers: [ ItemListService, MenuItem, ITEMS ]
    });
  });

  it('should ...', inject([ItemListService], (service: ItemListService) => {
    expect(service).toBeTruthy();
  }));
});

This defines MenuItem:

export class MenuItem {
    name: string;
    link: string;
}

This defines ITEMS: import {MenuItem} from './../classes/menu-item';

export var ITEMS: MenuItem[] = [
    {name: 'Vehicles', link: '/vehicles'},
    {name: 'Gateways', link: '/gateways'},
    {name: 'Statuses', link: '/statuses'},
    {name: 'Logs', link: '/logs'}
]

When I run the test, I get the following errors in the browser console:

FAILED ItemListService should ...

and

enter image description here

So why do I have such errors? And what is the solution for the test?

+4
source share
2 answers

This is such an annoying mistake, I thought that I would include another subtle reason to look in your specification. In my case, I indicated "provider" instead of "provide" below

 TestBed.configureTestingModule({
      providers: [{provider: ApplicationActions, useClass: ActionMock}]

, , ​​ "no" " ",

Failed: Invalid provider for the NgModule 'DynamicTestModule' - only instances of Provider and Type are allowed, got: [?[object Object]?, ...]
+9

:
:

- "ItemsListService"
, , - "ItemListService"

, .

+6

Source: https://habr.com/ru/post/1668515/


All Articles