jeudi 18 mai 2017

How to set up a test for a component that includes a router with angular?

I have an application built with angular(typescript) I am trying to setup a test for app.component. There is a router-outlet in this component. And I want to know how to properly set up a test in this situation.

app.component.spec.ts looks like this

import { TestBed, async } from '@angular/core/testing';
import { inject } from '@angular/core/testing';
import { AppComponent } from './app.component';
import { Component, OnInit, Injectable } from '@angular/core';
import { Router, RouterOutlet } from "@angular/router";
import {DriftInfoDisplayComponent} from "./components/drift-info-display/drift-info-display.component";
import {DriftInfoService} from "./services/http-services/drift-info.service";
import {DriftInfo} from "./model/DriftInfo";

describe('AppComponent', () => {
  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [
        AppComponent,
        DriftInfoDisplayComponent
      ],
      providers:[
        DriftInfoService,
        Router,
        RouterOutlet
      ]
    }).compileComponents();
  }));

  it('should create the app', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app).toBeTruthy();
  }));

  it(`should have as title 'app works!'`, async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    const app = fixture.debugElement.componentInstance;
    expect(app.title).toEqual('app works!');
  }));

  it('should render title in a h1 tag', async(() => {
    const fixture = TestBed.createComponent(AppComponent);
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
    expect(compiled.querySelector('h1').textContent).toContain('app works!');
  }));
});

But I get this fail when running ng test

AppComponent should create the app Failed: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?).

Aucun commentaire:

Enregistrer un commentaire