【发布时间】:2018-01-22 19:34:17
【问题描述】:
我想为我的解析器编写一个单元测试,它需要在其构造函数中采用ActivatedRouteSnapshot,如下所示:
export class MyResolver {
constructor () {
// ...
}
resolve (private route: ActivatedRouteSnapshot) {
callFoo(route.params.val);
}
};
但在我的单元测试中,为激活的路线快照提供模拟数据的最佳方式是什么?当我尝试使用我需要的属性创建一个对象时,我收到一个错误,它不能被强制转换为 ActivatedRouteSnapshot:
it('should call foo', inject([MyResolver], async (myResolver: MyResolver) => {
const mockRoute = {
params: {
val: '1234'
};
};
sinon.spy(callFoo);
myResolver.resolve(mockRoute); // This is the line that errors
expect(callFoo.calledWith('1234')).to.be.true;
}));
错误:
Type '{ params: { val: string; }; }' cannot be converted to type 'ActivatedRouteSnapshot'.
如何提供一个模拟的 ActivatedRouteSnapshot 以传递给我的解析器?
【问题讨论】:
标签: angular unit-testing angular-router