1 votes

Angular6, Test Error , Router

I'm testing with Karma v1.4.1 - connected and it doesn't pass the user component through the router ->

import { Router } from '@angular/router';

@Component({
  selector: 'app-user',
  templateUrl: './user.component.html',
  styleUrls: ['./user.component.scss']
})
export class UserComponent implements OnInit {
  public userList = [];
  constructor(private service: DataProviderService, private router: Router) { }

  ngOnInit() {
    this.getUsers();
  }

  private getUsers = function () {
    this.service.getUsers().subscribe(
      data => {
        this.userList = data;
      }, err => {
        console.log('err', err);
      }
    );
  };

  public detailUser(user: any) {
    this.service.userContext = user;
    this.router.navigate(['/detailComponent']);
  };

And his .spect.ts file I added the Router and the BrowserDynamicTestingModule (I read this one from somewhere else), the thing is that it still doesn't work.

import { UserComponent } from './user.component';
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DataProviderService } from '../data-provider.service';
import { BrowserDynamicTestingModule } from '@angular/platform-browser-dynamic/testing';
import { HttpClientTestingModule } from '@angular/common/http/testing';
import { Router } from '@angular/router';

describe('UserComponent', () => {
  let component: UserComponent;
  let fixture: ComponentFixture<UserComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [UserComponent],
      imports: [
        HttpClientTestingModule,
        // para el router
        Router,
        BrowserDynamicTestingModule
      ],
      providers: [DataProviderService]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(UserComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });
});

Chrome 74.0.3729 (Windows 10.0.0): Executed 5 of 5 (1 FAILED) (0.454 secs / 0.395 secs)

Failure ->

UserComponent should create
Failed: Unexpected value 'Router' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

Error: Unexpected value 'Router' imported by the module 'DynamicTestModule'. Please add a @NgModule annotation.

Expected undefined to be truthy.

1voto

Pablo Lozano Points 23403

The class Router is part of the module RouterModule you have to add it to the list of imported modules (and not to the list of imported modules). Router which is not a module). But in addition, to do unit tests, you have to use a mock created specifically for this purpose: RouterTestingModule .

import { RouterTestingModule } from '@angular/router/testing';

...
imports: [
    HttpClientTestingModule,
    // para el router
    RouterTestingModule,  //AQUI DEBES IMPORTAR El MODULO, NO LA CLASE
    BrowserDynamicTestingModule
  ],

0 votes

keeps giving error : Error: No provider for Router!

0 votes

Corrected, see if it works now

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X