如何使用茉莉花大理石测试ngrx效果?

时间:2019-04-25 06:05:44

标签: jasmine karma-runner ngrx jasmine-marbles

我无法测试NgRx效果。你能帮我吗?

朋友,请帮助我。我想测试一下效果,但是我不能。我收到错误消息“预期$ [0] .notification.value.payload是一种对象,但是是User({名称:'1212',角色:['somerole']})”。 我不明白怎么了。

效果:

@Injectable({
  providedIn: 'root'
})
@Injectable()
export class AuthEffects {

  constructor(
    private actions$: Actions,
    private rootService: RootService,
    private router: Router,
  ) {
  }

  @Effect()
  authUser$: Observable<any> = this.actions$.pipe(
    ofType(authActions.FETCHING),
    map((action: authActions.Fetching) => action.payload),
    switchMap((paylod: UserRequest) => this.rootService.login(paylod)
        .pipe(
          map((value) => {
            const {sub, authorities} = value;
            this.router.navigate(['/customers-list']);
            return new authActions.Success(new User(sub, authorities));
          }),
          catchError(() => of(new authActions.Fail('wrong username or password')))
        )
    )
  );
}

规格:

describe('AuthEffects', () => {
  let effects: AuthEffects;
  let rootService: jasmine.SpyObj<RootService>;
  let actions: Observable<any>;

  beforeEach(() => {
    TestBed.configureTestingModule({
      imports: [HttpClientTestingModule, RouterTestingModule],
      providers: [
        RootService,
        AuthEffects,
        provideMockActions(() => actions),
        {
          provide: RootService,
          useValue: {
            login: jasmine.createSpy()
          }
        }
      ]
    });

    effects = TestBed.get(AuthEffects);
    rootService = TestBed.get(RootService);
  });

  it('should work', () => {
    const userRequest: UserRequest = {
      name: '1212',
      password: 'alsj'
    };
    const userResponse: UserResponse = {
      sub: '1212',
      authorities: ['somerole']
    };
    const editedUser: User = {
      name: '1212',
      roles: ['somerole']
    };
    const action = new authActions.Fetching(userRequest);
    const completion = new authActions.Success(editedUser);

    actions = hot('-a', {a: action});
    const response = cold('-a|', {a: userResponse});
    rootService.login.and.returnValue(response);
    const expected = cold('--b', {b: completion});

    expect(effects.authUser$).toBeObservable(expected);
  });
});

我尝试根据一些示例进行制作,但有任何错误。

2 个答案:

答案 0 :(得分:1)

您必须对在测试中设置期望块的方式进行较小的更改。请尝试以下操作:

opencv

代替

effects.authUser$.subscribe(actionSent => {
 expect(actionSent).toBeObservable(expected)
})

我希望这对您有用。

答案 1 :(得分:0)

似乎构造函数可以解决这个问题。如果我更改没有构造函数的效果代码-它的工作原理

  @Effect()
  authUser$: Observable<any> = this.actions$.pipe(
    ofType(authActions.FETCHING),
    map((action: authActions.Fetching) => action.payload),
    switchMap((paylod: UserRequest): any => this.rootService.login(paylod)
        .pipe(
          map((value: UserResponse) => {
            const {sub, authorities} = value;
            return new authActions.Success({
              name: sub,
              roles: authorities
            });
          }),
          catchError(() => of(new authActions.Fail('wrong username or password')))
        )
    )
  );