微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从角度8迁移到角度10后,Ngrx效果测试用例无法正常工作

如何解决从角度8迁移到角度10后,Ngrx效果测试用例无法正常工作

我正在开发角度应用程序,根据要求,我已将我的应用程序升级到角度版本10,在迁移了为ngrx效果编写的单元测试用例后,这里给出了以下屏幕快照链接

角度效果测试用例失败

enter image description here

Autheffect测试用例: 编写用于登录操作的测试用例

import { Testbed,inject,fakeAsync } from '@angular/core/testing';
import { provideMockActions } from '@ngrx/effects/testing';
import { Observable,of,ReplaySubject } from 'rxjs';
import { hot,cold } from 'jasmine-marbles';
import { AuthEffects } from './auth.effects';
import { AuthConfig,AuthConfigProvider } from '../config/auth.config';

import { AuthService } from '../auth.service';
import { getEffectsMetadata,EffectsMetadata } from '@ngrx/effects';
import { RouterTestingModule } from '@angular/router/testing';
import * as AuthActions from './auth.actions';
import { addMatchers,initTestScheduler } from 'jasmine-marbles';
 

describe('AuthEffects',() => {
  let msalAuthServiceStub: Partial<AuthService>;
  msalAuthServiceStub = {
    loginPopup: () => of(),logout: () => of(),acquiretokenSilent: () => of(),acquiretokenPopup: () => of()
  };

  let actions$: Observable<any>;
  let effects: AuthEffects;
  let service: AuthService;
  let Metadata: EffectsMetadata<AuthEffects>;
 

  beforeEach(() => {
    Testbed.configureTestingModule({
        imports: [RouterTestingModule],providers: [
        {
          provide: AuthService,useValue: msalAuthServiceStub
        },AuthConfigProvider,AuthEffects,provideMockActions(() => actions$)
      ]
    });
    initTestScheduler();
    addMatchers();
    effects = Testbed.get(AuthEffects);
    service = Testbed.get(AuthService);
    Metadata = getEffectsMetadata(effects);

  });

  it('should be created',() => {
    expect(effects).toBeTruthy();
    expect(service).toBeTruthy();
  });

  

  it('should expect AcquiretokenSilent action on login',() => {
    
    const action = new AuthActions.LoginStart();
    debugger;
    actions$ = hot('-a',{ a: action });
    
    const response$ = cold('-a|',{ a: of() });
    spyOn(service,'loginPopup').and.returnValue(response$); 

    const completed$ = new AuthActions.AcquiretokenSilent();
    const expected$ = cold('--b',{ b: completed$ });
    
    expect(effects.authLogin$).toBeObservable(expected$);
    
  });

auth.effects.ts: 用于检查登录的身份验证效果

import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { Actions,Effect,ofType } from '@ngrx/effects';
import { tap,map,switchMap,catchError } from 'rxjs/operators';
import { of } from 'rxjs';
import { Location } from '@angular/common';

import { AuthService } from '../auth.service';
import * as AuthActions from './auth.actions';
import {User } from '../user.model'

@Injectable()
export class AuthEffects {
  @Effect()
  authLogin$ = this.actions$.pipe(
    ofType(AuthActions.LOGIN_START),switchMap(() =>
      this.authService.loginPopup().pipe(
        map(id_token => new AuthActions.AcquiretokenSilent()),catchError(error => of(new AuthActions.LoginFail({ error })))
      )
    )
  );

Auth.action.ts

import { Action } from '@ngrx/store';

export const LOGIN_START = '[Auth] Login';
export const LOGIN_SUCCESSFUL = '[Auth] Login Succesful';
export const LOGIN_FAIL = '[Auth] Login Fail';
export const ACQGUIRE_TOKENSILENT = '[Auth] AcquiretokenSilent';
export const ACQGUIRE_REFRESHTOKEN = '[Auth] AcquireRefreshToken';
export const ACQGUIRE_TOKENPOPUP = '[Auth] AcquiretokenPopup';
export const logoUT = '[Auth] logout';

export class LoginStart implements Action {
    readonly type = LOGIN_START;
  }
 
export class LoginSuccessful implements Action {
    readonly type = LOGIN_SUCCESSFUL;
    constructor(
      public payload: {
        name: string;
        accesstoken: string;
        expirationDate: Date;
      }
    ) {}
  }
 
export class LoginFail implements Action {
  readonly type = LOGIN_FAIL;
    constructor(public payload: { error: any }) {}
  }
  
export class AcquiretokenSilent implements Action {
    readonly type = ACQGUIRE_TOKENSILENT;
}
export class AcquireRefreshToken implements Action {
  readonly type = ACQGUIRE_REFRESHTOKEN;
}

export class AcquiretokenPopup implements Action {
    readonly type = ACQGUIRE_TOKENPOPUP;
  }
  
  export class logout implements Action {
  readonly type = logoUT;
}

export type AuthActions = 
| LoginStart
| LoginSuccessful
| LoginFail
| AcquiretokenSilent
| AcquireRefreshToken
| AcquiretokenPopup
| logout ;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。