A.Roussange
A.Roussange

Reputation: 73

Angular 2 - Error Invalid provider unit test

I'm working on a project with Angular 2 RC4 generate by Angular-Cli, and i have the same error on each of my unit test.

Error: Invalid provider - only instances of Provider and Type are allowed, got: [object Object] (line 170)

Here is a simple example of one spec :

import {addProviders,inject} from "@angular/core/testing";
import {AppComponent} from "./app.component";

beforeEach(() => {
  addProviders([AppComponent])
});

describe('Component : App', () => {
  it('should created an instance',
    inject([AppComponent], (app:AppComponent) => {
      expect(app).toBeTruthy();
    }));

});

This error appears on each spec which need providers.

Here is the component of app.component.ts

import { Component } from '@angular/core';
import  { EnvComponent } from "./env/env.component"

// Routage
import { ROUTER_DIRECTIVES } from '@angular/router';

@Component({
  moduleId: module.id,
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css'],
  directives: [ROUTER_DIRECTIVES, EnvComponent],
})


export class AppComponent {

}

PS : The tests worked correctly in Angular 2 RC3

Upvotes: 2

Views: 1158

Answers (1)

Thierry Templier
Thierry Templier

Reputation: 202138

You need to only use providers or types as parameters of the addProviders method:

addProviders([AppComponent])

It seems that AppComponent isn't a class... What is the content of the app.component module?

Upvotes: 1

Related Questions