I have a simple router guard and I am trying to test the canActivate( route: ActivatedRouteSnapshot, state: RouterStateSnapshot )
. I can create the ActivatedRouteSn
I managed to do it slightly differently but it should work for you :
...
let mockSnapshot:any = jasmine.createSpyObj("RouterStateSnapshot", ['toString']);
@Component({
template: ' '
})
class RoutingComponent { }
@Component({
template: ''
})
class DummyComponent { }
describe('Testing guard', () => {
beforeEach(() => TestBed.configureTestingModule({
imports: [
RouterTestingModule.withRoutes([
{path: 'route1', component: DummyComponent},
{path: 'route2', component: DummyComponent},
...
])
],
declarations: [DummyComponent, RoutingComponent],
providers: [
GuardClass,
{provide: RouterStateSnapshot, useValue: mockSnapshot}
]
}).compileComponents());
it('should not allow user to overcome the guard for whatever reasons',
inject([GuardClass], (guard:GuardClass) => {
let fixture = TestBed.createComponent(RoutingComponent);
expect(guard.canActivate(new ActivatedRouteSnapshot(), mockSnapshot)).toBe(false);
})
...