6

I'm trying to test an Angular Resolver which accesses children routes param.

My guard works fine but I cannot create an unit test easily because I cannot create an ActivatedRouteSnapshot with children routes (read only property).

My resolver

@Injectable({
    providedIn: 'root'
})
export class MyResolverGuard implements Resolve<string> {

    constructor() {
    }

    resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): string {
        return route.firstChild.paramMap.get('my-param');
    }
}

My test :

    it('should resolve chilren route params', () => {
        guard = TestBed.get(MyResolverGuard);
        const route = new ActivatedRouteSnapshot();
        // Cannot assign children because it's a read only property
        route.children = [...];
        const myResolverParams = guard.resolve(route, null);
    });

Is there other any other ways than using mock ActivatedRouteSnapshot ?

Does my approach to test guard is good ?

Thanks for sharing your strategy.

nircraft
  • 8,242
  • 5
  • 30
  • 46
Martin Choraine
  • 2,296
  • 3
  • 20
  • 37
  • Could you explain why you want to test the guard without mocking the ```ActivatedRouteSnapshot```? – Erbsenkoenig Aug 05 '19 at 06:14
  • That's because I don't want bypass logic inside `ActivatedRouteSnapshot`. For exemple `children` routes will set `firstChild` property. My approach may be wrong, I just wanted to look for an alternative than mocking. – Martin Choraine Aug 05 '19 at 06:59
  • 3
    Imho maybe it would be a better approach to test that inside an integration test rather than a unit test. – Erbsenkoenig Aug 05 '19 at 07:03
  • If you want a populated ActivatedRouteSnapshot object, I think you would need to mock your routes too (with the Guard) and perform a `router.navigate()`. As you have noted, by mocking ActivatedRouteSnapshot object, you can't manipulate read-only properties. ActivatedRouteSnapshot [represents a snapshot/state of an ActivatedRoute object at a point in time](https://stackoverflow.com/questions/46050849/what-is-the-difference-between-activatedroute-and-activatedroutesnapshot-in-angu). – terahertz Aug 05 '19 at 07:24
  • By performing a `router.navigate()` to a **mock route in RouterTestModule that is also defined with the Guard** will allow the guard's injected ActivatedrouteSnapshot to have various read-only properties (i.e. firstChild, children, pathFromRoot) that would otherwise be null in a mocked `new ActivatedRouteSnapshot()` object. I have no idea how your guard works so I am not able to provide code samples. Do post more code example (like how your routes look like) if you think it would help people to understand your context and answer your question... – terahertz Aug 05 '19 at 07:33

0 Answers0