I have a simple router guard and I am trying to test the canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot )
. I can create the ActivatedRouteSn
If the purpose is just to pass the mock to the guard, it's not necessary to use createSpyObj
, as suggested in other answers. The simplest solution is just to mock only required fields, which are used by canActivate
method of your guard. Also, it would be better to add type safety to the solution:
const mock = (obj: Pick): T => obj as T;
it('should call foo', () => {
const route = mock({
params: {
val: '1234'
}
});
const state = mock({
url: "my/super/url",
root: route // or another mock, if required
});
const guard = createTheGuard();
const result = guard.canActivate(route, state);
...
});
If you don't use the state snapshot, just pass null
instead
const result = guard.canActivate(route, null as any);