title: Angular 2 Testing the Router Module
body: <p>I am attempting to test my Router in Angular 2.2.1 to ensure the proper routes are set up and going to the expected location.  I've done Google research and have read and re-read the app.component.router.spec.ts file from the example at <a href="https://angular.io/docs/ts/latest/guide/testing.html" rel="nofollow noreferrer">https://angular.io/docs/ts/latest/guide/testing.html</a> and am still falling short.</p>
  
  <p>I found a really good answer here that was easy to understand and appeared obvious:
  <a href="https://stackoverflow.com/questions/39623722/angular-2-final-release-router-unit-test/39624731#39624731">Angular 2 Final Release Router Unit Test</a></p>
  
  <p>So using that, I created this as a test for my own Router:</p>
  
  <pre><code>import { Component } from '@angular/core';
  import { Location } from '@angular/common';
  import { Router } from '@angular/router';
  import { RouterTestingModule } from '@angular/router/testing';
  import { fakeAsync, async, inject, TestBed, getTestBed } from '@angular/core/testing';
  import { By } from '@angular/platform-browser';
  
  @Component({
      template: '&lt;router-outlet&gt;&lt;/router-outlet&gt;'
  })
  class RoutingComponent { }
  
  @Component({
      template: ''
  })
  class DummyComponent { }
  
  describe('component: RoutingComponent', () =&gt; {
      let location, router;
  
      beforeEach(() =&gt; {
          TestBed.configureTestingModule({
              imports: [RouterTestingModule.withRoutes([
                  { path: 'home', component: DummyComponent },
                  { path: 'not-exists', component: DummyComponent }
              ])],
              declarations: [RoutingComponent, DummyComponent]
          });
      });
  
      beforeEach(inject([Router, Location], (_router: Router, _location: Location) =&gt; {
          location = _location;
          router = _router;
      }));
  
      it('should fail, but does not', async(() =&gt; {
          let fixture = TestBed.createComponent(RoutingComponent);
          fixture.detectChanges();
          router.navigate(['not-exists']).then(() =&gt; {
              expect(location.path()).toBe('/not-exists');
              console.log('after expect');
          });
      }));
  });
  </code></pre>
  
  <p>The problem I'm having is that I don't understand how this is testing my real router. As you can see I created a path in here called 'not-exists' that goes to a dummy component and, as the name indicates, this route does not exist in my real router. This makes me believe that it really isn't testing anything except for the paths that I put here.</p>
  
  <p>So my questions are:</p>
  
  <ol>
  <li>How can I test my real router so that if routes change this test breaks?</li>
  <li>How can I test my router where it goes to one path but redirects to another? (For example I have a path '/' that redirects to '/home')</li>
  <li>Is it possible to test my real router where a particular path goes to a particular component?</li>
  </ol>
  
  <p>Thanks!</p>