title: Angular Unit testing : this.router.myMethod is not a function
body: <p>Am worrking on unit testing , under Angular 4.0.0</p>
  
  <p>My test case is the following :</p>
  
  <p><strong>test.spec.ts :</strong></p>
  
  <pre><code>        // Test Suite of Que-Again features
              describe('QueueAgainComponent features', () =&gt; {
  
                  it('should navigate', () =&gt; {
                      comp.goToPrevious(); // HERE IS THE PROBLEM
                      expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/pilote'], {skipLocationChange: true});
                      sharedclientService.user.isAdviser = true;
                      comp.goToPrevious();
                      expect(mockRouter.navigate).toHaveBeenCalledWith(['/home/advisor'], {skipLocationChange: true}); 
         });
  </code></pre>
  
  <p>Under the config part i ve mocked my <strong>RouteNavigator</strong> service like the following :</p>
  
  <pre><code>        let mockRouter = {
              navigate: jasmine.createSpy('navigate')
          };
      providers: [{provide: RouteNavigator, useValue: mockRouter}]
  </code></pre>
  
  <p>My test fails throwing tghis error :  </p>
  
  <blockquote>
    <p>TypeError: this.router.myMethod is not a function</p>
  </blockquote>
  
  <p>The log file says that the problem points on 98:18 which is exactly this line :</p>
  
  <p>comp.goToPrevious();</p>
  
  <p>Therefore <strong>goToPrevious()</strong> is implemented in my component like this : </p>
  
  <pre><code>      goToPrevious() {
          this.routeNavigator.myMethod();
        }
  </code></pre>
  
  <p>And <strong>routeNavigator</strong> refers to a custom service used to deal with custom redirection , like this :</p>
  
  <pre><code>**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()  // HERE IS MY METHOD
       {
  
          if(this.sharedclientService.user != null &amp;&amp; this.sharedclientService.user.isAdviser==null){
              this.navigateTo('/home/blank');
              this.sharedclientService.setCurrentRole('');
          }
  
          else  if (this.sharedclientService.user != null &amp;&amp; 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;
        }
       }
  </code></pre>
  
  <p><strong>Any ideas about what's the problem or how to deal whith it ??</strong></p>