简体   繁体   English

Redux异步操作测试的目的是什么?

[英]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. 所以在文档http://rackt.org/redux/docs/recipes/WritingTests.html中 ,我们模拟了服务器请求。

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? 我们正在测试是否发生了正确的操作顺序,给出200的正确响应?

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. 这些类型的测试可帮助您避免此类问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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