我在 reactjs 中测试我的异步操作时遇到问题。这是我的代码:
export function fillStoryBoard(snippetsLink, channel, uniqueStoryObj, isUniqueStorySearch) {
return function(dispatch){
for (var i = 0; i < snippetsLink.length; i++) {
loadStorySnippet(channel, snippetsLink[i], (i + 1),dispatch);
}
}
}
function loadStorySnippet(channel,snippetsLink,indexValue,dispatch){
return axios.get(ServiceUrls.prototype.getServicesDfltUrl()+snippetsLink)
.then(response => {
dispatch({
type: "FILL_STORYBOARDS",
payload: {
"channelName": channel,
"storiesSnippet": response.data,
"order":indexValue
}
});
}).catch(function(response){
});
}
所以我的 asnc 调用在一个未导出的函数中,并且有一个函数在 for 循环中调用这个 asnc 函数。
现在这是我的测试:
it('should dispatch type: FILL_STORYBOARDS with what is returned from server as a payload', () => {
nock(ServiceUrls.prototype.getServicesDfltUrl())
.get('/snippet/111')
.reply(200, {"id":1,"headline":"",
"label":"Other",
"imgUrl":"",
"postDate":0});
const expectedActions = [
{ "type": "FILL_STORYBOARDS","payload": { "channel": "",
"storiesSnippet": {"id":1,"headline":"",
"label":"Other","imgUrl":"" +
"",
"postDate":0},
"order":1 } }
]
const store = mockStore();
return store.dispatch(fillStoryBoard(["snippet/111"], "")).then(() => {
expect(store.getActions()[0].type).to.equal(expectedActions[0].type);
})
})
现在的问题是运行测试时我得到:
TypeError: Cannot read property 'then' of undefined
知道如何解决这个问题吗?