简体   繁体   中英

What is the purpose of Redux Async Action Testing?

Could someone explain why we test asynchronous actions? What are we hoping to ensure works?

So in the docs, http://rackt.org/redux/docs/recipes/WritingTests.html , we mock the server request.

nock('http://example.com/')
  .get('/todos')
  .reply(200, { todos: ['do something'] })

const expectedActions = [
  { type: types.FETCH_TODOS_REQUEST },
  { type: types.FETCH_TODOS_SUCCESS, body: { todos: ['do something']  } }
]
const store = mockStore({ todos: [] }, expectedActions, done)
store.dispatch(actions.fetchTodos())

So we aren't testing to see if we contact the server. We are testing to see if the correct sequence of actions takes place, given a response of 200 correct?

Yes, the point of this type of test isn't to see if your network requests are working correctly but to ensure that the sequence of actions that you expect to fire is actually firing. It can also be used to make sure that the data you get back from the server is the correct format your front end expects.

This may seem trivial if you write the tests after you've implemented the code, but say that down the road, you or another developer decides to make some slight tweaks to those actions or remove one entirely. Worst case scenario, your code fails silently, leaving you to wonder what went wrong. These types of tests help you avoid that kind of problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM