angular - Angular 5 单元测试路由器导航
问题描述
我想测试这个功能。当返回 true 我想在这个路由器中导航。
我的组件代码:
onLogin() {
this.loading = true;
this.auth.login(
this.loginForm.controls['username'].value,
this.loginForm.controls['password'].value)
.subscribe(
result => {
if (result === true) {
this.router.navigate(['/home']);
} else {
this.loading = false;
}
}
);
}
我试过这个测试,结果是真的
it('should call service.login when onLogin', done => {
const mock = [];
spyOn(component['auth'], 'login').and.callThrough()
component['auth'].login('abcs', '1').subscribe(
result => {
if (result === true) {
}
console.log(result)
done();
});
component.onLogin();
})
解决方案
推荐阅读
- google-sheets-api - 如何使用电子表格.create() 方法指定工作表 API 在 Google 驱动器中创建新电子表格的位置?
- javascript - ExpressJS:在运行时动态添加路由
- reactjs - 如何从 react-color 更改 ChromePicker 中色板的背景颜色?
- python - 如何通过defusedxml从AWS SQS API字节xml字符串响应中读取所有行,而不仅仅是第一行?
- php - 会话在 Laravel 7 中的代码中不会持续存在
- java - Javamoney.moneta.Money 序列化/反序列化为 null 尽管我有 jackson-datatype-money 依赖项
- python - 在删除停用词后从列创建词汇表
- reactjs - 如何使用带有 2 个输入的搜索栏通过 React 调用 API?
- spring - SonarQube 分析问题
- vue.js - v-bind 的 Vue JS 问题