angular - 如何在 Jasmine 单元测试中模拟位置重定向,Angular?
问题描述
我想测试这个功能:
onLinkClick() {
if (this.congratsType === 'warning') {
this.location.back();
} else {
window.location.href = this.urlNol;
}
}
我已经尝试过这个:
let loc: Location;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
declarations: [EndFlowComponent],
providers: [
{ provide: location, useValue: window.history },
],
}).compileComponents();
}));
beforeEach(() => {
loc = jasmine.createSpyObj('Location', ['back']);
});
it(' redirect on Link click', () => {
const congratsTypes = ['success', 'error' , 'info'];
// tslint:disable-next-line: no-increment-decrement
for (let i = 0; i < congratsTypes.length; i++) {
component.congratsType = congratsTypes[i];
component.onLinkClick();
expect(loc.back).toHaveBeenCalledTimes(1);
}
})
当我运行测试时,浏览器重新定位到外部页面。但我想在不实际导航到该 URL 的情况下运行它。
解决方案
推荐阅读
- google-chrome - 如何在 Chrome 信息亭模式下允许网络摄像头访问?
- c++ - 这个特定用例的一个很好的排序算法
- vba - 设置作为自定义对象的属性时,对象不支持此属性或方法
- sql - 使用相关子查询从 Select 更新并在 PostgreSQL 中加入
- python - 在 Outlook 中从 Excel 附件创建数据框
- go - 在 Heroku 上获取零星的“http:代理错误:读取 tcp:i/o 超时”
- python - 采样 Pandas Dataframe 的最快方法?
- r - R按列表中的列名索引不起作用
- c++ - 在运行时使用编译器
- c - printf 无法识别 C 指针