Jasmine is not recognizing the getCurrentNavigation() function

Asked

Viewed 26 times

0

I’m trying to test a component that uses function getCurrentNavigation() of the Router to pick up data via navigation. My component is working normally with the expected purpose when I run with ng serve, but my test is saying:

Component2component > should create Typeerror: this.router.getCurrentNavigation is not a Function

This is the way I get the date from the router:

constructor(public router: Router) {
  if (
    this.router.getCurrentNavigation() &&
    this.router.getCurrentNavigation().extras
  ) {
    console.log(this.router.getCurrentNavigation().extras);
  }

My test:

describe('Component2Component', () => {
  let component: Component2Component;
  let fixture: ComponentFixture<Component2Component>;
  const routerSpy = jasmine.createSpyObj('Router', ['navigate']);

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [Component2Component],
      imports: [CommonModule],
      providers: [{ provide: Router, useValue: routerSpy }]
    }).compileComponents();
  }));

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

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

Because my test is not recognizing this function?

1 answer

0


If anyone goes through this problem, I resolved importing the module RouterTestingModule in the list of modules Imports of the test and removing the declaration of routerSpy that I created (The module now manages the functions and not the Spy created):

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [Component2Component],
      imports: [CommonModule, RouterTestingModule],
    }).compileComponents();
  }));

And then the function becomes accessible.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.