我有以下组件方法:
onSignup() {
if (this.form.valid) {
let email = this.form.value.email;
let password = this.form.value.password;
this.usersService.signup(email, password)
.then(() => {
console.log('1')
this.router.navigate(['/app/home']);
})
.catch(err => {
this.mdlSnackbarService.showToast(err);
});
}
}
以下单元测试:
it('should navigate on success', async(() => {
spyOn(usersService, 'signup').and.returnValue(Promise.resolve());
component.form.setValue({'email': 'mail@mail.com', 'password': '123456'});
component.onSignup();
fixture.whenStable().then(() => {
console.log('2')
expect(router.navigate).toHaveBeenCalledWith(['/app/home']);
})
}))
我添加了几个console.log
以找出失败的原因,结果证明1
从未打印过,所以它不会等待承诺得到解决,尽管使用fixture.whenStable()
。这是我的beforeEach
:
beforeEach(inject([Router],(_router: Router) => {
router = _router;
router.navigate = jasmine.createSpy('navigate');
fixture = TestBed.createComponent(SignupComponent);
component = fixture.componentInstance;
fixture.detectChanges();
}));
有谁知道我在这里做错了什么?