单位测试还原器,测试每个切片减少或组合还原器



假设我有一个还原文件reducers/group1.js这样的

export default combineReducers({
  A: combineReducers({ A1, A2 }),
  B: reducerB,
  C: reducerC
})

测试每个切片还原器(A1,A2,还原和还原)和测试合并的一个?

是否有区别?
import group1 from 'reducers/group1'
describe('reducers', () => {
  describe('group1', () => {
    it('should provide the initial state', () => {
      expect(group1(undefined, {})).to.equal({ group1: { A: { ... }, B: ... } })
    })
    it(...)
    // ...
  })
})

import { A1, A2, reducerB, reducerC } from 'reducers/group1'
describe('reducers', () => {
  describe('group1', () => {
    describe('A1', () => {
      it('should provide the initial state', () => {
        expect(A1(undefined, {})).to.equal(0) // if A1 is just a number
      })
    })
    describe('A2', () => { ... })
    describe('reducerB', () => { ... })
    describe('reducerC', () => { ... })
  })
})

您的第二个示例通常更好,因为它允许进行更简单的单元测试。我可以想象一个场景,开发人员可能想为Reducer C编写一堆测试,而无需了解Reducers AB。第二个代码示例允许该开发人员编写C测试套件,而不必担心AB是什么。在重写测试时,如果还原器的行为发生了巨大改变,这也有帮助:所有这些测试都活在一个地方,而不是分散在测试文件上。

但是,在某些情况下,您想为整个还原器编写测试。例如,如果您有全球重置操作,则需要测试整个还原器对该动作的响应正确,而不是为每个还原器编写单个测试。大多数时候,为单个还原器编写测试可能会更干净。

最新更新