错误:在使用nock和mock-store的Redux应用程序中的异步操作测试中,可能无法定义操作

时间:2017-03-22 19:58:58

标签: unit-testing testing redux jestjs

有点难过这个。按照Redux文档为我的异步操作(docs here)设置测试,我收到错误:

  

动作可能不是未定义的。 (在发送时(node_modules / redux-mock-store / lib / index.js:35:19))

测试此操作:

export const FETCH_TRANSACTIONS = 'FETCH_TRANSACTIONS'

function fetchTransactionsSuccess (transactions) {
  return {
    type: FETCH_TRANSACTIONS,
    payload: transactions
  }
}

export const fetchTransactions = () => dispatch => axios.get('/api/transactions')
  .then(transactions => dispatch(fetchTransactionsSuccess(transactions)))
  .catch(err => dispatch(handleErr(err)))

这就是测试本身。任何帮助都会很棒。我一直盯着这眼看着我的眼睛受伤了。

import configureMockStore from 'redux-mock-store'
import thunk from 'redux-thunk'
import * as actions from '../../client/actions/actionCreators'
import nock from 'nock'
import expect from 'expect'

const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)

describe('async actions', () => {
  afterEach(() => {
    nock.cleanAll()
  })

  it('dispatches FETCH_TRANSACTIONS when data is returned', () => {
    nock('http://localhost:3000/')
      .get('/api/transactions')
      .reply(200, [
        {
          "_id": "588900efdf9d3e0905a2d604",
          "amount": 4.50,
          "name": "Cashew Nuts",
          "__v": 0,
          "date": "2017-01-25T00:00:00.000Z",
          "user": "58c2a33cc6cd5a5d15a8fc0c"
        },
        {
          "_id": "58890108df9d3e0905a2d605",
          "amount": 6.25,
          "name": "Monmouth Coffee",
          "__v": 0,
          "date": "2017-01-25T00:00:00.000Z",
          "user": "58c2a33cc6cd5a5d15a8fc0c"
        }
      ])

    const expectedActions = [
      {
        type: actions.FETCH_TRANSACTIONS,
        payload: [
          {
            "_id": "588900efdf9d3e0905a2d604",
            "amount": 4.50,
            "name": "Cashew Nuts",
            "__v": 0,
            "date": "2017-01-25T00:00:00.000Z",
            "user": "58c2a33cc6cd5a5d15a8fc0c"
          },
          {
            "_id": "58890108df9d3e0905a2d605",
            "amount": 6.25,
            "name": "Monmouth Coffee",
            "__v": 0,
            "date": "2017-01-25T00:00:00.000Z",
            "user": "58c2a33cc6cd5a5d15a8fc0c"
          }
        ]
      }
    ]

    const store = mockStore({ transactions: [] })
    console.log(actions)
    return store.dispatch(actions.fetchTransactions())
      .then(() => {
        expect(store.getActions()).toEqual(expectedActions)
      })
  })
})

UPDATE handleErr函数返回setCurrentUser,这是另一个动作(原始动作使用dispatch调用:

export function handleErr (err) {
  if (err.status === 401 || err.status === 404) {
    localStorage.removeItem('mm-jwtToken')
    setAuthToken(false)
    return setCurrentUser({})
  }
}

1 个答案:

答案 0 :(得分:1)

axios known issue请求nock fetchTransactions。因此,我相信您的catch动作创建者中的承诺链属于handleErr条款。请检查您的undefined功能,它是否返回有效的操作?我敢打赌它会返回from html.parser import HTMLParser class MyHTMLParser(HTMLParser): def handle_data(self, data): print("Encountered some data :", data) parser = MyHTMLParser() parser.feed('<html><head><title>Test</title></head>' '<body><h1>Parse me!</h1></body></html>') ,这就是您收到此错误消息的原因。