在TS文件中,我具有以下功能:
login() {
if (this.email.valid && this.password.valid) {
const emailValue = this.email.value;
const passwordValue = this.password.value;
if (this.authService.verifyLogin(emailValue, passwordValue) === 'passwordIsMatch') {
this.router.navigate(['/success']);
} else if (this.authService.verifyLogin(emailValue, passwordValue) === 'passwordNotMatch') {
this.wrongPassword = true;
this.wrongEmail = false;
} else {
this.wrongEmail = true;
}
}
}
基本上,如果authService返回'passwordIsMatch',则我正在呼叫router.navigate ['success']
电子邮件值和密码值来自FormControls:
email = new FormControl('', [Validators.required, Validators.email]);
password = new FormControl('', [Validators.required, Validators.minLength(8), SymbolOrNumberValidator()]);
现在进行单元测试,使用玩笑,这是我到目前为止尝试过的:
it('should navigate to success if email & password are truthy', fakeAsync(() => {
const router = TestBed.get(Router);
const authService = TestBed.get(AuthenticationService);
const spy = jest.spyOn(router, 'navigate');
let serviceRes = authService.verifyLogin('real-mail@gmail.com', 'realPW123')
component.login();
tick();
expect(spy).toHaveBeenCalledWith(['/success']);
}));
在这种情况下,serviceRes等于passwordIsMatch(它是一个字符串),这正是该函数将路由路由到“成功”路由所需要的。
因此,当前的问题是我无法告诉函数我的服务返回“ passwordIsMatch” 缺什么 ? 谢谢!