title: angular 2 testing w/router
body: <p>I have a component and when a user logs in it routes to a url called <code>/dashboard</code> I am really struggling figuring out why I am getting the following error.</p>
  
  <pre><code>cannot read property 'args' of undefined
  </code></pre>
  
  <p>I have been following the official docs on testing with router found here <a href="https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component" rel="nofollow noreferrer">https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component</a>
  but it doesnt seem to help. Half my problem is I dont quite understand all the code in the doc. Here is my unit test for the route</p>
  
  <pre><code> beforeEach(async(()=&gt;{
  
        class AuthStub{
          private loggedin: boolean = false;
              login(){
                return this.loggedin = true;
  
              }
        };
  
        class RouterStub{
          navigateByUrl(url:string){return url;}
        }
  
      TestBed.configureTestingModule({
        declarations: [ HomePageContentComponent ],
        providers:[
          {provide: Auth, useClass:AuthStub},
          {provide: Router, useClass: RouterStub}
          ]
      })
      .compileComponents()
    }));
  
    beforeEach(()=&gt;{
  
        fixture = TestBed.createComponent(HomePageContentComponent);
        comp = fixture.componentInstance;
  
        de = fixture.debugElement.query(By.css('.loginbtn'));
        el = de.nativeElement;
        fixture.detectChanges();
  
      });
  
      it('Should log in and navigate to dashboard', inject([Router],(router:Router)=&gt;{
  
        const spy = spyOn(router, 'navigateByUrl');
  
        el.click();
  
        const navArgs = spy.calls.first().args[0];
  
        expect(navArgs).toBe('/dashboard');
  
      }))      
  }); 
  </code></pre>
  
  <p>So my question is what is this line of code doing...</p>
  
  <pre><code>const navArgs = spy.calls.first().args[0];
  </code></pre>
  
  <p>and how can I fix my problem?</p>
  
  <p><strong>Service added</strong></p>
  
  <pre><code>@Injectable()
  export class Auth { 
      lock = new Auth0Lock('fakefakefakefakefake', 'fake.auth0.com', {
           additionalSignUpFields: [{
              name: "address",                              
              placeholder: "enter your address"
          }],
          theme: {
              primaryColor:"#b3b3b3",
          },
           languageDictionary: {
              title: "FAKE"
          }
      });
  
      userProfile: any;
  
      constructor(private router: Router) {
          this.userProfile = JSON.parse(localStorage.getItem('profile'));
  
          this.lock.on("authenticated", (authResult) =&gt; {
              localStorage.setItem('id_token', authResult.idToken);
  
              this.lock.getProfile(authResult.idToken, (error, profile) =&gt; {
                  if (error) {
  
                      alert(error);
                      return;
                  }
  
                  profile.user_metadata = profile.user_metadata || {};
                  localStorage.setItem('profile', JSON.stringify(profile));
                  this.userProfile = profile;
              });
              this.router.navigate(['/dashboard']);
          });
      }
  
      public login(){
          this.lock.show();
  
      };
  
      public authenticated() {
          return tokenNotExpired();
      };
  
      public logout() {
          localStorage.removeItem('id_token');
          localStorage.removeItem('profile');
          this.router.navigate(['/logout']);
      };
  }
  </code></pre>