I am currently working on unit testing in Angular 4.0.0.
Here is my test case:
test.spec.ts :
// Test Suite of Que-Again features
describe('QueueAgainComponent features', () => {
it('should navigate', () => {
comp.goToPrevious(); // THIS IS WHERE THE ISSUE LIES
expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/pilote'], {skipLocationChange: true});
sharedclientService.user.isAdviser = true;
comp.goToPrevious();
expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/advisor'], {skipLocationChange: true});
});
In the configuration part, I have mocked my RouteNavigator service like this:
let mockRouter = {
navigate: jasmine.createSpy('navigate')
};
providers: [{provide: RouteNavigator, useValue: mockRouter}]
My test fails with the following error:
TypeError: this.router.myMethod is not a function
The log file indicates that the problem is at line 98:18, which corresponds to this line:
comp.goToPrevious();
The implementation of goToPrevious() in my component looks like this:
goToPrevious() {
this.routeNavigator.myMethod();
}
The routeNavigator refers to a custom service used for handling custom redirection, defined as follows:
**Route-navigator.service.ts**
import { LoginComponent } from '../../login/login.component';
import { SharedclientService } from '../../service/sharedclient.service';
import { Injectable } from '@angular/core';
import { Router, ActivatedRoute } from '@angular/router';
@Injectable()
export class RouteNavigator {
constructor(private router: Router, private sharedclientService: SharedclientService, private activatedRoute: ActivatedRoute) { }
accessAdvancedSearchFromRegistration = false;
accessSyntheseWhileGdfaShown = false;
track360View = false;
oldUrl: string;
private rdvReferer: string;
public navigateTo(url: string) {
this.oldUrl = this.router.url;
this.router.navigate([url], { skipLocationChange: true });
}
public myMethod() // MY METHOD IS HERE
{
if(this.sharedclientService.user != null && this.sharedclientService.user.isAdviser==null){
this.navigateTo('/home/blank');
this.sharedclientService.setCurrentRole('');
}
else if (this.sharedclientService.user != null && this.sharedclientService.user.isAdviser) {
this.navigateTo('/home/advisor');
this.sharedclientService.setCurrentRole('VENDEUR');
} else {
this.navigateTo('/home/pilote');
this.sharedclientService.setCurrentRole('PILOTE');
}
}
public goToNextAdvancedSearch() {
if (this.accessAdvancedSearchFromRegistration || !this.sharedclientService.user.isAdviser) {
this.navigateTo('/home/registration');
}
else {
this.track360View = true;
this.navigateTo('/home/view-360');
}
}
public exitAdvancedSearch() {
if (this.accessAdvancedSearchFromRegistration) {
this.navigateTo('/home/registration');
}
else {
this.goToHomeAccordingToProfile();
}
}
goToRdv(url?:string) {
if(!url)
this.rdvReferer = this.router.url;
else this.rdvReferer=url;
this.navigateTo('/home/appointment');
}
exitRdv() {
this.navigateTo(this.rdvReferer);
}
goToBlank() {
this.navigateTo('/home/blank');
}
public url() {
return this.router.url;
}
}
Any suggestions on how to resolve this issue or deal with it?