试验方法
public onSubmit(registerData: RegisterDataModel): void {
this.registrationService.registerWithEmailAndPassword(registerData).then((msg: string[]) =>
this.router.navigate(['/completeSignUp']).then(() => {
msg.forEach(singleMessage => this.notificationService.primary(singleMessage));
}))
.catch((msg) => msg.forEach(singleMessage => {
this.notificationService.danger(singleMessage);
}));
}
我想测试一下
router.navigate
在我的方法中被调用。现在我想嘲笑我
service.registerWithEmailAndPasswort
答应我,但我不能嘲笑。
我的规范文件
//Stubs
const routerStub: Router = jasmine.createSpyObj('Router', ['navigate']);
const registryStub: RegistrationService = jasmine.createSpyObj('RegistrationService', ['registerWithEmailAndPassword']);
单元测试
it('should navigate on promise - success', () => {
(<jasmine.Spy>registryStub.registerWithEmailAndPassword).and.callThrough();
const spy = (<jasmine.Spy>routerStub.navigate);
component.onSubmit({username: null, email: null, password: null, passwordConfirm: null, termsAndCondition: null});
expect(spy).toHaveBeenCalledWith(['/completeSignUp']);
});
出现的错误是:
TypeError: Cannot read property 'then' of undefined
有谁能恰当地模仿这项服务吗?
编辑
我也曾试图嘲弄这个承诺,比如:
(<jasmine.Spy>registryStub.registerWithEmailAndPassword)
.and.returnValue(new Promise(() => Promise.resolve()));
但它仍然让我:
Expected spy Router.navigate to have been called with [ [ '/completeSignUp' ] ] but it was never called.