我刚开始接受测试,所以我甚至不确定这是否是我所想要的#39;要测试,但是这里:
我跟随https://github.com/reactjs/redux/blob/master/docs/recipes/WritingTests.md中的示例,了解如何为异步动作创建者编写测试。以下是我测试的代码:
export function receiveRepresentatives(json) {
return {
type: RECEIVE_REPRESENTATIVES,
representatives: json.objects
}
}
export function getRepresentatives (zipcode) {
return dispatch => {
dispatch(changeFetching())
return fetch('/api/representatives' + zipcode)
.then(response => response.json())
.then(json => dispatch(receiveRepresentatives(json)))
}
}
我的测试框架是带有nock和configureMockStore的mocha / chai。我想用nock模拟我对/ api /代表的号召,但我无法弄清楚如何。
const middlewares = [ thunk ]
const mockStore = configureMockStore(middlewares)
describe('async actions', () => {
afterEach(() => {
nock.cleanAll()
})
it('creates RECEIVE_REPRESENTATIVES when fetching representatives has been done', (done) => {
nock('http://localhost')
.get('/api/representatives')
.reply('200', { objects: { name: 'Barbara Lee'} } )
const expectedActions = [
{ type: RECEIVE_REPRESENTATIVES, representatives: { objects: { name: 'Barbara Lee'} } }
]
const store = mockStore({}, expectedActions, done)
store.dispatch(getRepresentatives(94611))
.then(() => {
const actions = store.getActions()
expect(actions[0].type).toEqual(receiveRepresentatives())
done()
})
})
})