0
votes

1 spec, 1 failure Spec List | Failures OverviewComponent should create Failed: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?). Error: Can't resolve all parameters for Router: (?, ?, ?, ?, ?, ?, ?, ?). at syntaxError (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:215:1) at CompileMetadataResolver._getDependenciesMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10807:1) at CompileMetadataResolver._getTypeMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10700:1) at CompileMetadataResolver._getInjectableTypeMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10922:1) at CompileMetadataResolver.getProviderMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10931:1) at http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10869:1 at Array.forEach () at CompileMetadataResolver._getProvidersMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10829:1) at CompileMetadataResolver.getNgModuleMetadata (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:10548:1) at JitCompiler._loadModules (http://localhost:9876/karma_webpack/webpack:/node_modules/@angular/compiler/fesm5/compiler.js:22567:1)

============================================


describe('OverviewComponent', () => {
  let component: OverviewComponent;
  let fixture: ComponentFixture<OverviewComponent>;
  let router : Router

 
  const fakeActivatedRoute = {
    snapshot: { data: {  } }
  } as ActivatedRoute;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      imports:[
        SharedModule,
        FlexLayoutModule,
       
       // RouterModule.forRoot(routes),
        RouterTestingModule
      ],
       
      declarations: [ OverviewComponent,
      FooterComponent,
      LoginComponent,
      ChangePasswordComponent,
      ForgotPasswordComponent,
      AppComponent],

      providers:[
      HttpClient,
      HttpHandler,
      DataService,
      NgxSpinnerService,
      Router,
      RouterModule,
      EmitterService,
      {provide: ActivatedRoute, 
        useValue: fakeActivatedRoute}
      ]
    })
    .compileComponents();
    

  }));

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


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

 
});
1
Please provide code of should create test for OverviewComponent. It seems that you are not providing RouterDumbo
Please have a look on codeRazor B
@RazorB : your component code also. how can we know unit test behavior if we dont know the component :)Shashank Vivek

1 Answers

1
votes

You need create mock to Service Router.

You can also just use the RouterTestingModule and just spyOn the navigate function like this...

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

import { MyModule } from './my-module';
import { MyComponent } from './my-component';

describe('something', () => {

    let fixture: ComponentFixture<LandingComponent>;
    let router: Router;

    beforeEach(() => {

        TestBed.configureTestingModule({
            imports: [
                MyModule,
                RouterTestingModule.withRoutes([]),
            ],
        }).compileComponents();

        fixture = TestBed.createComponent(MyComponent);
        router = TestBed.get(Router);

    });

    it('should navigate', () => {
        const component = fixture.componentInstance;
        const navigateSpy = spyOn(router, 'navigate');

        component.goSomewhere();
        expect(navigateSpy).toHaveBeenCalledWith(['/expectedUrl']);
    });
});

Font: stackoverflow