棱角分明 – Ngrx如何测试后卫

前端之家收集整理的这篇文章主要介绍了棱角分明 – Ngrx如何测试后卫前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想测试这个简单的守卫
既可以激活又可以负载
怎么管理呢?
我做了第一步管理注入的商店

@Injectable({
  providedIn: 'root'
})
export class AuthGuard implements CanActivate,CanLoad {
    constructor(private store: Store<AuthState>) {}

    canActivate(): Observable<boolean> {
        return this.store.pipe(
            select(selectIsAuthenticated),map(isValidToken => {
                if (!isValidToken) {
                    this.store.dispatch(new logout());
                    return false;
                }
                return true;
            }),take(1)
       );
    }

    canLoad(): Observable<boolean> {
        return this.store.pipe(
            select(selectIsAuthenticated),take(1)
        );
    }
}

我的第一步

export const authReducer: ActionReducerMap<{}> = {
  status: {}
};
describe('AuthGuard',() => {
  let store: Store<{}>;
  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [StoreModule.forRoot({}).forFeature('auth',authReducer)],providers: [Store,AuthGuard]
    });
    store = TestBed.get(Store);
  });

  it('should ...',inject([AuthGuard],(guard: AuthGuard) => {
    expect(guard).toBeTruthy();
  }));
});

但是测试canActivate和canLoad呢?
我要嘲笑选择和如何?

解决方法

我不太了解Angular会引导你去模拟select函数.我也无法从您的代码示例中了解select函数的来源.但是Angular文档包含了在测试中使用模拟的内容.文档是否不足以满足您的需求?或者你需要帮助理解文档?链接https://angular.io/guide/testing

猜你在找的Angularjs相关文章