我在 angular 内部有以下实现service
,即所谓的AService
.
startDoSth(param: string) {
const queryParams = this.constructQueryParams(param);
window.location.href = environment.foo.barUrl + '?' + queryParams;
}
定义environment
如下
export const environment = {
foo: {
barUrl: https://dummy-url/baz
}
}
我的想法是,无论何时startDoSth
调用,都window.location.href
必须调用一次。这个想法的实现
it('should start doing something', () => {
AService.startDoSth('foo');
verify(window.location.href).once();
});
但是,我得到了错误
TypeError:无法读取未定义的属性“getAllMatchingActions”
我猜这个实例window
没有被模拟,然后测试使用真实实例,可能有不匹配的东西!?
因此,我有一个想法,使用ts-mockito
stub 对象window
,然后我可以验证行为甚至结果。不幸的是,我不知道该怎么做。有什么建议么?