How do I Mock RouterStateSnapshot for a Router Guard Jasmine test

后端 未结 4 1067
生来不讨喜
生来不讨喜 2021-02-05 05:23

I have a simple router guard and I am trying to test the canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot ). I can create the ActivatedRouteSn

4条回答
  •  盖世英雄少女心
    2021-02-05 05:55

    Based on a previous question I had about Router I tried this...

    let mockSnapshot: any;
    ...
    mockSnapshot = jasmine.createSpyObj("RouterStateSnapshot", ['toString']);
    ...
    TestBed.configureTestingModule({
      imports: [RouterTestingModule],
      providers:[
        {provide: RouterStateSnapshot, useValue: mockSnapshot}
      ]
    }).compileComponents();
    ...
    let test = guard.canActivate(
      new ActivatedRouteSnapshot(),
      TestBed.get(RouterStateSnapshot)
    );
    

    The problem I now have is that I need the toString here mockSnapshot = jasmine.createSpyObj("RouterStateSnapshot", ['toString']);. This is because jasmine createSpyObj requires at least one mocked method. Since I am not testing the side effects of RouterStateSnapshot, this seems like extra work for nothing.

提交回复
热议问题