我正在使用Jest测试redux动作,当我尝试测试默认动作时,它会抛出错误
Expected value to equal: {"payload": {"male": "mancha"}, "type": "actions/change_gender"} Received: [Function anonymous]
它似乎发送了函数,而不是值。
测试 change_gender.js
import changeGender, { CHANGE_GENDER } from '../change_gender';
const payload = {
type: CHANGE_GENDER,
payload: {
male: 'mancha'
}
};
describe('actions', () => {
it('should Change the ', () => {
const expectedAction = {
type: payload.type,
payload: payload.payload
};
expect(changeGender('male', 'mancha')).toEqual(expectedAction)
});
});
行动change_gender.js
import toggleToolTip from './toggle_tooltip'; // eslint-disable-line
export const CHANGE_GENDER = 'actions/change_gender';
export default(radioType, type) => (dispatch) => {
dispatch({
type: CHANGE_GENDER,
payload: {
[radioType]: type
}
});
};
答案 0 :(得分:0)
您应该在change_gender.js
<强> change_gender.js 强>:
import toggleToolTip from './toggle_tooltip'; // eslint-disable-line
export const CHANGE_GENDER = 'actions/change_gender';
export default(radioType, type) => (dispatch) => {
return dispatch({
type: CHANGE_GENDER,
payload: {
[radioType]: type
}
});
};
答案 1 :(得分:0)
正如Chen-tai所说,从发货回来将有助于测试目的。
您看到[Function]返回的原因是您的操作是返回函数的函数。
(radioType, type) => (dispatch) => { ... }
第一组参数,然后是胖箭是一个匿名函数。然后返回另一个匿名函数,该函数将 dispatch 函数作为其参数。因此,如果我们两次调用该操作,提供模拟调度函数,我们将恢复预期的操作!
const action = (radioType, type) => (dispatch) => {
return dispatch({
type: "CHANGE_GENDER",
payload: {
[radioType]: type
}
});
};
console.log(
action('male', 'mancha')((action) => action)
)
然后我们可以写出测试:
行动change_gender.js
import toggleToolTip from './toggle_tooltip'; // eslint-disable-line
export const CHANGE_GENDER = 'actions/change_gender';
export default(radioType, type) => (dispatch) => {
return dispatch({
type: CHANGE_GENDER,
payload: {
[radioType]: type
}
});
};
测试change_gender.js:
import changeGender, { CHANGE_GENDER } from '../change_gender';
const payload = {
type: CHANGE_GENDER,
payload: {
male: 'mancha'
}
};
describe('actions', () => {
it('should Change the ', () => {
const expectedAction = {
type: payload.type,
payload: payload.payload
};
expect(changeGender('male', 'mancha')((payload) => payload).toEqual(expectedAction)
});
});