angular - 如何测试 locationStrategy.onPopState?
问题描述
我需要测试我的功能,但我遇到了困难。
我无法执行 100% 的覆盖,我的困难与通过函数 onPopState 调用的函数 history.pushState 有关。
有人能帮我吗?
// This line is never being covered by my tests
this.locationStrategy.onPopState(() => {
history.pushState(null, null, url);
});
我的服务代码:
export class MyService {
constructor(private readonly locationStrategy: LocationStrategy) { }
blockNavigator(url: string) {
history.pushState(null, null, urlAtual);
// This line is never being covered by my tests
this.locationStrategy.onPopState(() => {
history.pushState(null, null, url);
});
}
}
我的服务测试代码:
describe('MyService ', () => {
let myService: MyService;
let locationStrategyMock: LocationStrategy;
beforeEach(() => {
TestBed.configureTestingModule({
providers: [
BloqueioNavegacaoService,
{
provide: LocationStrategy,
useValue: jasmine.createSpyObj('LocationStrategy', ['onPopState'])
}
]
});
myService = getTestBed().inject(MyService);
locationStrategyMock = getTestBed().inject(LocationStrategy);
});
describe('Methods:', () => {
it('.....', () => {
spyOn(myService , 'blockNavigator').and.callThrough();
locationStrategyMock.onPopState = jasmine.createSpy()
.and.callFake(() => {
// window.history.pushState(null, null, 'url');
});
myService.blockNavigator(url);
expect(myService.blockNavigator).toHaveBeenCalledTimes(1);
expect(window.history.state).toBeNull();
expect(window.history.length).toBeGreaterThanOrEqual(0);
});
});
解决方案
我认为您可以利用.calls.mostRecent().args[0]
获取函数参数的句柄并显式调用该函数然后断言。
看看下面,也有评论。
describe('MyService ', () => {
let myService: MyService;
let locationStrategyMock: LocationStrategy;
// Add this line
let mockLocationStrategy: jasmine.SpyObj<LocationStrategy>;
beforeEach(() => {
// Add this line
mockLocationStrategy = jasmine.createSpyObj('LocationStrategy', ['onPopState']);
TestBed.configureTestingModule({
providers: [
BloqueioNavegacaoService,
{
provide: LocationStrategy,
// change useValue here
useValue: mockLocationStrategy
}
]
});
myService = getTestBed().inject(MyService);
locationStrategyMock = getTestBed().inject(LocationStrategy);
});
describe('Methods:', () => {
it('.....', () => {
// This line is not needed, we call it explicitly
// spyOn(myService , 'blockNavigator').and.callThrough();
myService.blockNavigator(url);
// This line is not needed, we call it explicitly (asserting nothing)
// expect(myService.blockNavigator).toHaveBeenCalledTimes(1);
// get a handle of the function (first argument) of onPopState
// when it is called (i.e. () => {
// history.pushState(null, null, url);
// }
const callBackFunction = mockLocationStrategy.onPopState.calls.mostRecent().args[0];
// Call the function
callBackFunction();
// Hopefully the bottom assertions work
expect(window.history.state).toBeNull();
expect(window.history.length).toBeGreaterThanOrEqual(0);
});
});
推荐阅读
- reactjs - 将 create-react-app 部署到 Plesk Onyx
- java - 在运行我的 Android Studio 项目时,Java 编译器给出了这个错误
- javascript - 通过 .map 问题转换对象数组
- sql - SQL Server 2008 R2:查找两列之间的链接和链
- javascript - webpack css-loader 解析失败
- c++ - c++ 基于模板字符串字面量返回的函数指针
- java - SOCKET TCP android java to java -> 客户端在 out.println(message) 上发送错误消息
- r - 计算以相同的第一位数字,然后是第二位数字等开头的数字的出现次数
- javascript - 在平行表中对齐单元格高度
- php - 需要修复 mysqli_result 代码