unit-testing - Jasmine Karma 在角度 5 中调用登录组件
问题描述
我是 Jasmine 业力测试用例的新手。我尝试为登录组件编写业力测试用例。在那次点击中,提交按钮没有调用 component.ts 文件中的 onSubmit 方法
登录组件.ts
onSubmit() {
this.authService.login(this.data.username, this.data.password)
.delay(1000)
.subscribe(data => {
sessionStorage.setItem('token', JSON.stringify(data.token));
if (this.urlDirect !== null) {
window.location.href = this.sharedService.baseUrl + '/' + this.urlDirect;
} else { this.router.navigate(['/dashboard']); }
},
error => {
this.submitted = false;
setInterval(() => {
this.spinnerlogo = false;
}, 1000);
let i = 0;
const timer = setInterval(() => {
this.errorDiagnostic = 'Incorrect username or password.';
i++;
if (i === 10) {
clearInterval(timer);
this.spinnerlogo = false;
this.errorDiagnostic = null;
}
}, 500);
});
}
login.component.spec.ts
it(`entering value in username and password input controls`, () => {
userNameEl.nativeElement.value = 'admin';
passwordEl.nativeElement.value = 'admin';
fixture.detectChanges();
});
it('after entering value the button should enabled and click Action should happen', () => {
submitEl.triggerEventHandler('click', null)
// tslint:disable-next-line:no-unused-expression
expect(comp.onSubmit).toHaveBeenCalled;
// const loginButtonSpy = spyOn(comp, 'onSubmit');
// submitEl.triggerEventHandler('click', null);
// expect(loginButtonSpy).toHaveBeenCalled();
});
解决方案
推荐阅读
- java - 未找到类 Def,log4j2 中的渲染器映射
- mongodb - 如何查看地理距离
- typescript - 从 Typescript 中的 Promise 推断返回值
- javascript - 如何将 Chart.js 轴最小值设置为 0?
- django - 如何使用自动数据库路由为使用 PostgreSQL 的 Django 应用程序创建写入和读取副本
- sql - Oracle sql中从出生日期开始的年龄范围
- javascript - 是否有可能在进行操作后或在我的情况下在日历中创建事件然后页面使用 JavaScript 自动向下滚动?
- c# - 过滤大量收藏的最佳方法?
- django - 使用 Django Restframework 实现通知
- java - Cucumber JVM - 如何正确重用其他要素类中编写的方法?