为什么我的jest异步操作创建程序测试不起作用?

时间:2018-02-06 05:59:45

标签: unit-testing jest redux-mock-store

我是单元测试的新手,我正在尝试通过我的react-redux项目编写一些测试。

为什么这个测试不起作用,我怎么能通过?

这是测试。我想测试我的fetch posts动作创建者。这适用于小型博客应用程序。:

import configureStore from 'redux-mock-store'; // ES6 modules
import { findSinglePost, sendEdit, changeRedirect, checkBoxChange } from '../client/redux/actions/postActions';
import thunk from 'redux-thunk';
import axios from 'axios';

const middlewares = [thunk];
const mockStore = configureStore(middlewares);
describe('asynchronous action creators', () => {

  it('should fetch posts', () => {
    let store = mockStore({})

    //my async action creator. It uses mock data that's in the same folder.
    const fetchPosts = () => function(dispatch) {
      dispatch({type: 'FETCH_POSTS'});

      return axios.get('./MOCK.json').then((response) => {
        dispatch({type: 'FETCH_POSTS_FUFILLED', payload: response.data});
      }).catch((err) => {
        dispatch({type: 'FETCH_POSTS_REJECTED', payload: err});
      });
    };
    //this doesn't equal FETCH_POSTS_FUFILLED, it ends up equaling just "FETCH_POSTS"
    return store.dispatch(fetchPosts()).then(() => {
      const actions = store.getActions();
      expect(actions[0]).toEqual({type: 'FETCH_POSTS_FUFILLED'});
    })
  })
});

这是开玩笑的反馈。我希望它等于'FETCH_POSTS_'FUFILLED',但它返回'FETCH_POSTS'。 :

 FAIL  _test_\actions.test.js
  ● asynchronous action creators › should fetch posts

    expect(received).toEqual(expected)

    Expected value to equal:
      {"type": "FETCH_POSTS_FUFILLED"}
    Received:
      {"type": "FETCH_POSTS"}

    Difference:

    - Expected
    + Received

      Object {
    -   "type": "FETCH_POSTS_FUFILLED",
    +   "type": "FETCH_POSTS",
      }

      88 |     return store.dispatch(fetchPosts()).then(() => {
      89 |       const actions = store.getActions();
    > 90 |       expect(actions[0]).toEqual({type: 'FETCH_POSTS_FUFILLED'});
      91 |     })
      92 |   })
      93 | });

      at _test_/actions.test.js:90:26

 PASS  client\views\LoginPage\LoginPage.test.jsx

Test Suites: 1 failed, 1 passed, 2 total
Tests:       1 failed, 5 passed, 6 total
Snapshots:   0 total
Time:        1.49s
Ran all test suites related to changed files.

此外,如果你想尝试运行它,这里是项目的github repo

此外,如果行业中有一种标准的方式,如何做到这一点更为人所知,我会喜欢这个建议。

编辑:

当我将操作[0]更改为操作[1] 时,我收到此错误:

Expected value to equal:
  {"type": "FETCH_POSTS_FUFILLED"}
Received:
  {"payload": {Symbol(impl): {"message": "The string did not match the expected pattern.", "name": "SyntaxError", Symbol(wrapper): [Circular]}}, "type": "FETCH_POSTS_REJECTED"}

Difference:

- Expected
+ Received

  Object {
-   "type": "FETCH_POSTS_FUFILLED",
+   "payload": DOMException {
+     Symbol(impl): DOMExceptionImpl {
+       "message": "The string did not match the expected pattern.",
+       "name": "SyntaxError",
+       Symbol(wrapper): [Circular],
+     },
+   },
+   "type": "FETCH_POSTS_REJECTED",

以下是jest反馈的图片形式:

1 个答案:

答案 0 :(得分:2)

您正在使用的模拟商店将存储已对其进行的所有已调度的调用。在您的情况下,应进行两次调度调用,第一次调用FETCH_POSTS,第二次调用FETCH_POST_FULFILLEDFETCH_POST_REJECTED

因此,当您从模拟商店中检索已调度的操作时,第一个条目(您在expect中使用的)将是FETCH_POSTS。您应该根据您正在测试的函数中如何解析promise来检查数组中的第二个值,即FETCH_POSTS_FULFILLEDFETCH_POSTS_REJECTED