在 Epic 中测试 Post 请求

问题描述

我正在尝试测试具有 ajax 发布请求的史诗。

    test('test sucess login',async() => {
        const response = {
          username: 'admin',};
        nock('http://localhost:8080')
          .post('/login',Buffer.from(['admin','admin']).toString('base64'))
          .reply(200,response);

    const credentials = { username: 'admin',password: 'admin' };
    const action$ = ActionsObservable.of(login(credentials));

    const result$ = await loginEpic(action$);
    console.log(result$);
    result$.subscribe(actions => {
      expect(actions).toEqual([{
        type: 'LOGIN_SUCCESS',payload: response
      }]);
    });
 });

即使我将预期的操作类型更改为“LOGIN_FAIL”或将对象设为空,测试也始终通过。我还在史诗中放置了一个控制台日志,但它从未被调用过。

export const loginEpic: Epic<LoginActionTypes | CallHistoryMethodAction > = (action$) => action$.pipe(


filter(isOfType(LOGIN)),mergeMap(action => login(action.payload.credentials ).pipe(
    mergeMap(ajaxResponSEObject => {
      console.log('inside epic',ajaxResponSEObject);
      return of(
        loginSuccess(ajaxResponSEObject.response),push('/test')
      );
    }),catchError((error: AjaxError) => of(loginFail(error)))
  ))
);

这是登录功能

const login = (credentials: LoginCredentials): Observable<AjaxResponse> => {
  const { username,password } = credentials;
  return ajax({
    url: 'http://localhost:8080/authenticate',method: 'POST',headers: {
      Authorization: `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}}`
    }
  });
};

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)