我有一個組件,當用戶登錄它時,路由到一個名爲/dashboard
的網址我真的很努力弄清楚爲什麼我得到以下錯誤。角度2測試w /路由器
cannot read property 'args' of undefined
我一直在關注與路由器測試官方文檔這裏https://angular.io/docs/ts/latest/guide/testing.html#!#routed-component 找到,但它似乎沒有幫助。我的一半問題是我不太理解文檔中的所有代碼。這裏是我的路線單元測試
beforeEach(async(()=>{
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(()=>{
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)=>{
const spy = spyOn(router, 'navigateByUrl');
el.click();
const navArgs = spy.calls.first().args[0];
expect(navArgs).toBe('/dashboard');
}))
});
所以我的問題是什麼是這行代碼做...
const navArgs = spy.calls.first().args[0];
和我怎麼能解決我的問題?
服務添加
@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) => {
localStorage.setItem('id_token', authResult.idToken);
this.lock.getProfile(authResult.idToken, (error, profile) => {
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']);
};
}
@yurzui將我的服務添加到問題 – Bean0341