在角度4中测试路由守卫,如何模拟出重要的位

时间:2019-02-13 09:09:59

标签: angular

我想测试以下角度4中的路由防护:

@Injectable()

export class AdminGuard implements CanActivate, OnDestroy {
  @select(isAdminSelector)
  public isAdmin$: Observable<boolean>;

  canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Promise<boolean> {
    return new Promise((resolve, reject) => {
      let subscription: Subscription = this.isAdmin$.subscribe((isAdmin) => {
        if (isAdmin !== undefined) {
          resolve(isAdmin);
          subscription.unsubscribe();
        }
      });
    });
  }

  ngOnDestroy() {}

我已经了解到了

describe('canActivate isAdmin', () => {
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [NgxDatatableModule, RouterTestingModule, NgReduxTestingModule],
      declarations: [AdminGuard],
      providers: []
    }).compileComponents();
  });

  it('should return true for admin user', () => {
    const isAdminSelectorMock = MockNgRedux.getSelectorStub(isAdminSelector);

    isAdminSelectorMock.next(true);

    isAdminSelectorMock.complete();

    const authGuard = new AdminGuard();

    // how do I stub out active route and state arguments???
    expect(authGuard.canActivate( ).toEqual(true);
  });
});

但是我不知道如何取消活动路由。

0 个答案:

没有答案