加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

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

发布时间:2020-12-17 18:07:19 所属栏目:安全 来源:网络整理
导读:我想测试这个简单的守卫 既可以激活又可以负载 怎么管理呢? 我做了第一步管理注入的商店 @Injectable({ providedIn: 'root'})export class AuthGuard implements CanActivate,CanLoad { constructor(private store: StoreAuthState) {} canActivate(): Obse
我想测试这个简单的守卫
既可以激活又可以负载
怎么管理呢?
我做了第一步管理注入的商店

@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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读