我正在尝试按如下方式对redux saga进行测试,但我遇到了一个问题。我得到的错误是无法读取未定义的属性有效负载。我传递给saga函数的'message'var由于某种原因未定义,有人可以告诉我为什么吗?感谢
saga.spec.js
import test from 'tape'
import { put,take,call } from 'redux-saga/effects'
import { onCreateMessage } from '../src/common/sagas/messages'
import { addMessage,createMessage } from '../src/common/reducers/messages'
test('createMessage', assert => {
const gen = onCreateMessage()
var message = {
id: 1234,
channelID: "AA",
text: "text",
user: "user"
}
assert.deepEqual(
gen.next(message).value,
put(addMessage(message)),
'createMessage should dispatch addMessage action'
)
})
saga/index.js
export default function* rootSaga(){
yield [
takeEvery('CREATE_MESSAGE', onCreateMessage),
......
]
当我在下面控制记录的消息时,我得到'未定义'
export function* onCreateMessage(message) {
console.log(message)
yield put(addMessage(message.payload))
try {
yield call(fetch,'/api/newmessage',
{
method: 'post',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(message.payload)}
)
} catch (error){
throw error
}
}
我正在使用redux-actions中的actionCreator:
export const addMessage = createAction(ADD_MESSAGE);
答案 0 :(得分:3)
变化
const gen = onCreateMesaage()
到
const gen = onCreateMessage(message)
说明:
message
是传递给生成器函数的参数,而不是yield
结果。
以前的代码适用于产生的结果。 e.g
export function* onCreateMessage() {
const message = yield
console.log(message)
yield put(addMessage(message.payload))
}