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

Posted

技术标签:

【中文标题】错误:在带有 nock 和 mock-store 的 Redux 应用程序的异步操作测试中,操作可能未定义【英文标题】:Error: Action may not be undefined in async action testing in Redux app with nock and mock-store 【发布时间】:2017-08-15 03:35:19 【问题描述】:

有点难倒这个。按照 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)
      )
  )
)

更新 handleErr 函数返回 setCurrentUser,它是另一个动作(原来的动作用 dispatch 调用:

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

【问题讨论】:

【参考方案1】:

有 known issue 与 nock 模拟 axios 请求。所以我相信fetchTransactions 动作创建者中的承诺链属于catch 子句。请检查您的handleErr 函数,它是否返回有效的操作?我敢打赌它会返回undefined,这就是您收到此错误消息的原因。

【讨论】:

嗯,是的。感谢您的链接。 handleErr 函数返回 setCurrentUser 操作(catch 通过调度调用) @LeMoi setCurrentUser 仅当err.status 存在且等于401 或404 时才会返回。调试它的值,我确定它是undefined 认为你是对的!运行测试时如何调试?抱歉,刚开始测试。 是的,删除了 if 语句,现在它返回 localStorage is not defined。啊,我该如何处理? (我是否检查 localStorage 是否可用?):-) @LeMoi 看起来您正在使用jest 进行测试。在这种情况下,您可以关注this article。或者只是在这里和那里放置一对console.log

以上是关于错误:在带有 nock 和 mock-store 的 Redux 应用程序的异步操作测试中,操作可能未定义的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 Nock 测试请求中的错误?

错误:Nock:与请求不匹配

superagent 和 nock 如何协同工作?

Nock 不能与 axios 一起使用获取操作异步测试

nock 没有拦截我的请求

如何查看 nock 是不是匹配请求?