[英]Error: Action may not be undefined in async action testing in Redux app with nock and mock-store
bit stumped with this one. 有点难过这一件事。 Following the Redux docs to setup tests for my async actions ( docs here ), I am getting the error:
按照Redux文档为我的异步操作( docs此处 )设置测试后,出现错误:
Actions may not be an undefined.
动作可能不是未定义的。 (at dispatch (node_modules/redux-mock-store/lib/index.js:35:19))
(在调度时(node_modules / redux-mock-store / lib / index.js:35:19))
Testing this action: 测试此操作:
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)))
And this is the test itself. 这就是测试本身。 Any help would be amazing.
任何帮助都将是惊人的。 Been staring at this so long my eyes hurt.
凝视了这么久,我的眼睛受伤了。
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 The handleErr function returns setCurrentUser which is another action (which the original action called with dispatch: 更新handleErr函数返回setCurrentUser,这是另一个动作(原始动作通过dispatch调用:
export function handleErr (err) {
if (err.status === 401 || err.status === 404) {
localStorage.removeItem('mm-jwtToken')
setAuthToken(false)
return setCurrentUser({})
}
}
There is known issue with mocking axios
request with nock
. 使用
nock
axios
请求存在一个已知问题 。 So I believe that promise chain in your fetchTransactions
action creator falls to the catch
clause. 因此,我相信您的
fetchTransactions
动作创建者中的promise链属于catch
子句。 Please check your handleErr
function, is it returning valid Action? 请检查您的
handleErr
函数,它是否返回有效的Action? I bet it returns undefined
and that is why you have this error message. 我敢打赌它返回
undefined
,这就是为什么您有此错误消息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.