问题描述
it('should tick invalidLogin at error on login',() => {
var checker;
var targetobj = new Proxy(component,{
set: (target,key,value) => {
if (key == 'invalidLogin' && value == true) {checker = true;}
return true;
}
})
targetobj.onSubmit(); // empty form should cause error
expect(checker).toBeTruthy();
});
但问题是断言发生在之前属性被标记。 我怎样才能使断言在后面?
onSubmit(){
if (this.loginform.invalid) {
return;
}
this.isSubmitted = true;
const _id = this.f.username.value
const _pass = this.f.password.value
this.authService.login(_id,_pass)
.pipe(first())
.subscribe(
data => {
this.authService.checkAdmin().subscribe(data => {
if(this.authService.IsCurrentUserAdmin) this.router.navigate(['admin'])
});
},error => {
if(error['status']== 400) this.invalidLogin = true;
console.log(error)
},() => {
this.isSubmitted = false;
});
return
}
更新 我放弃了这个方法,而是使用 Spy 来模拟 onSubmit() 方法,这样我就可以不用通过服务来标记属性。
it('should tick invalidLogin at error on login',() => {
let button = fixture.debugElement.query(By.css('#form-login')).nativeElement;
spyOn(component,'onSubmit').and.callFake(() => {
component.invalidLogin = true;
})
button.click();
fixture.detectChanges();
let message = fixture.debugElement.query(By.css('#invalid-text')).nativeElement;
expect(message).toBeTruthy;
});
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)