测试Redux组合减速器 [英] Testing Redux combined reducers
问题描述
说我有几种减速器功能,我使用combineReducers(...)
将它们全部组合到一个减速器中,有没有一种方法可以测试组合的减速器实际包含哪些减速器?
Say I have several reducer functions and I combine them all into one reducer using combineReducers(...)
, is there a way of testing what reducers the combined reducer actually contains?
例如,如果我有这个:
import { combineReducers } from 'redux'
const reducer1 = (state, action) => {...}
... (more reducers, etc)
const rootReducer = combineReducers({
reducer1,
reducer2,
reducer3
})
export default rootReducer
我可以用Mocha和Expect.js编写测试,使我能够检查rootReducer
是否包含"c3"吗?这有可能吗?
Can I write a test with Mocha and Expect.js that will enable me to check if the rootReducer
contains say reducer2
? Is this even possible?
我当前设置项目的方式是,每个reducer都位于单独的文件中,然后导入到文件中,其中combineReducers(...)
函数用于将它们组合在一起.我正在测试所有单个减速器,以检查它们是否做了应做的事情,但我也认为测试组合减速器以确保包含所有其他应有的减速器是一个好主意(以防万一我忘记了例如添加一个.)
The way I currently have my project set up is that each reducer is in a separate file and is then imported into the file where the combineReducers(...)
function is used to combine them all. I am testing all the individual reducers to check that they do what they should, but I also thought it would be a good idea to test the combined reducer to make sure that it contains all the other reducers that it should (in case I forget to add one for example).
谢谢
推荐答案
您正在测试IMO错误的东西.您应该相信combineReducers()
函数可以完成它的工作(应该在Redux分发测试中对其进行测试).但是,您可以创建一个方法,该方法将返回带有减速器的对象,使其合并以作为参数传递给combineReducers()
.该方法可以并且应该进行测试.
You are testing the wrong thing IMO. You should trust that the combineReducers()
function does what it should (it should be tested in Redux distrubution tests). But you can create a method that will return the object with reducers to combine to pass as the parameter to combineReducers()
. That method can and should be tested.
这篇关于测试Redux组合减速器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!