javascript - 创建 Redux 多动作集成测试
问题描述
我有一个 Redux 应用程序,我想在其中创建一个集成测试来涵盖一些复杂的交互。下面是我的功能代码,但我想知道是否有更好或更惯用的方法来实现相同的目标。
我的方法如下:
- 添加记录每个动作和应用动作后状态的中间件
- 在应用程序中手动执行所需的步骤
- 将记录的对象数组转储
{ action, state }
到 JSON 文件中 - 运行一个集成测试:
- 读取 JSON 文件
- 从动作/状态数组中的第一项建立初始状态(忽略第一个动作)
- 通过根 reducer 运行每个后续操作以获取新状态,并根据 JSON 文件中记录的值检查新状态
中间件如下所示:
const actionStateTestMiddleware = (store) => (next) => (action) => {
if (window && !window.actionStateTestLog) {
window.actionStateTestLog = [];
}
if (action) {
console.log('Logging action ', action.type);
}
const result = next(action);
window.actionStateTestLog.push({
action,
state: store.getState()
});
return result;
};
测试看起来像:
describe('some integration test', () => {
let tests;
before(async() => {
let buffer;
try {
buffer = await fsp.readFile(path.join(
__dirname,
'../path/to/integrationTest.json'
));
} catch (err) {
console.error('Unable to read test case JSON file');
throw err;
}
tests = JSON.parse(buffer.toString());
});
it('should maintain the expected state through all actions', () => {
let state = tests[0].state;
for (let t = 1; t < tests.length; t++) {
state = rootReducer(state, tests[t].action);
assert.deepStrictEqual(state, tests[t].state);
}
});
});
上述方法有效,但感觉远非理想。主要问题是:
- 开发者必须临时添加自定义中间件
- JSON 文件是为每个操作重复的整个存储,因此即使对于有限的测试用例也有大约 50k 行
- 任何影响商店任何部分的代码更改都会使此测试无效,因此需要:
- 重新运行生成测试的手动过程,尽管再次执行完全相同的步骤是不可行的
- 自动从测试用例重新生成 JSON 文件,查看更改的差异,并使用最佳判断来接受或拒绝。对于重大变化,这种差异可能难以理解。
有一个更好的方法吗?
解决方案
推荐阅读
- reactjs - 在反应中从登录重定向到注册表单
- javascript - 在循环内的函数调用之间添加延迟
- node.js - Child_process 在服务器上无法正常工作,挂载了 linux 服务
- java - 如何在android studio上创建一个新表到数据库中?
- c - C typedef 正确用例
- javascript - 如何使用 Swiper.js 在活动幻灯片上启动动画
- python - 使用 Kubernetes 客户端 Python 删除部署在 AKS 中的 POD 中的容器
- c# - 如何检查网络图像是否存在并且服务器进行重定向?
- python - PyGObject:libpango-1.0.so.0:未定义符号:g_ptr_array_copy
- node.js - 包中捆绑的本地依赖项在 npm install 上抛出错误