首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Ngrx + Jasmin测试问题(actual.subscribe不是一个函数)

Ngrx + Jasmin测试问题(actual.subscribe不是一个函数)
EN

Stack Overflow用户
提问于 2021-09-30 19:31:05
回答 1查看 122关注 0票数 0

在为我们在angular应用程序中编写的auth guard实现Spec文件时,给了我一个错误,我无法思考发生了什么。

代码语言:javascript
运行
复制
Auth Guard -------------> should return FALSE if the user state is not logged in FAILED
        TypeError: actual.subscribe is not a function
            at VirtualAction.<anonymous> (node_modules/jasmine-marbles/es6/index.js:55:1)
            at VirtualAction._execute (node_modules/rxjs/_esm2015/internal/scheduler/AsyncAction.js:51:1)
            at VirtualAction._execute (node_modules/rxjs/_esm2015/internal/scheduler/VirtualTimeScheduler.js:59:1)
            at VirtualAction.execute (node_modules/rxjs/_esm2015/internal/scheduler/AsyncAction.js:39:1)
            at TestScheduler.flush (node_modules/rxjs/_esm2015/internal/scheduler/VirtualTimeScheduler.js:16:1)
            at TestScheduler.flush (node_modules/rxjs/_esm2015/internal/testing/TestScheduler.js:107:1)
            at toBeObservableComparer (node_modules/jasmine-marbles/es6/index.js:77:1)
            at <Jasmine>
            at UserContext.<anonymous> (src/app/core/guards/auth.guard.spec.ts:55:37)

以上是我在为auth.guard.spec.ts和auth.guard.ts粘贴以下代码时所面临的问题

代码语言:javascript
运行
复制
import { TestBed } from '@angular/core/testing';
import { combineReducers, Store, StoreModule } from '@ngrx/store';
import { cold } from 'jasmine-marbles';
import { addMatchers, initTestScheduler } from 'jasmine-marbles';
import { RouterTestingModule } from '@angular/router/testing';
import { provideMockStore } from '@ngrx/store/testing';

import { AuthGuard } from './auth.guard';
import * as fromRoot from '../../app-state';
import * as fromAuth from '../../modules/auth/store/auth.reducer';
import * as fromAuthActions from '../../modules/auth/store/auth.actions';
import { User } from '../../modules/auth/login/login.model';
import { LogInComponent } from '../../modules/auth/login/login.component';

describe('Auth Guard', () => {
    let guard: AuthGuard;
    let store: Store<any>;
    const initialState = { loggedIn: false };
    beforeEach(() => {
        initTestScheduler();
        addMatchers();
        TestBed.configureTestingModule({
            imports: [
                RouterTestingModule.withRoutes([
                    { path: 'auth', component: LogInComponent }
                ]),
                StoreModule.forRoot({
                    ...fromRoot.reducers,
                    auth: combineReducers(fromAuth.reducer)
                })
            ],
            providers: [
                AuthGuard,
                provideMockStore({ initialState })
            ]
        });
        store = TestBed.inject(Store);
        spyOn(store, 'dispatch').and.callThrough();
        guard = TestBed.inject(AuthGuard);
    });

    it('-------------> should return FALSE if the user state is not logged in', () => {
        const expected = cold('(a|)', { a: false });
        expect(guard.canActivate()).toBeObservable(expected);
    });
});

guard.ts

代码语言:javascript
运行
复制
import { Injectable } from '@angular/core';
import { Router, CanActivate } from '@angular/router';
import { Observable } from 'rxjs';
import { Store } from '@ngrx/store';
import { AppState } from 'src/app/app-state';
import * as fromAuth from '../../modules/auth/store/auth.reducer';

@Injectable()
export class AuthGuard implements CanActivate {
  authState: Observable<fromAuth.AuthState>;
  constructor(private store: Store<AppState>, private router: Router) { }
  canActivate(): boolean {
    // Check if user is authenticated or not i.e. only logged in users havce access to todos page
    const authState = this.store.select(fromAuth.selectAuthState);
    authState.subscribe(state => {
      if (!state.isAuthenticated) {
        this.router.navigate(['auth']);
        return false;
      }
    });

    return true;
  }
}
EN

回答 1

Stack Overflow用户

发布于 2021-10-04 15:23:54

我认为由于您的canActivate返回的是boolean而不是Observable<boolean>,这可能是问题所在。

尝试这样更改您的canActivate

代码语言:javascript
运行
复制
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
....
canActivate(): Observable<boolean> {
    // Check if user is authenticated or not i.e. only logged in users have access to todos page
    return this.store.select(fromAuth.selectAuthState).pipe(
      map(state => {
        if (!state.isAuthenticated) {
          this.router.navigate(['auth']);
          return false;
        }
        return true;
      }),
    );
  }

现在,我们将返回一个可观察对象,然后在您的测试中,expect(guard.canActivate()).toBeObservable(...)应该可以工作。在此之前,guard.canActivate()boolean而不是Observable

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69397918

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档