Let's assume I have StartCommandHandler
which has responsibility to create some file with required files. But for doing this I have to give him a set of sub-responsibilities, like:
- 检查FTP中是否存在文件
- 如果不是,则将文件从多个来源下载到temp文件夹
- 然后在文件夹中执行一些脚本
- 然后在脚本执行后读取生成的文件
- 然后从该文件夹创建zip
- 然后删除该文件夹
- 然后更新数据库
作为该命令处理程序的结果,我们正在创建包含所有必需文件的文件夹。现在,该文件夹已准备好进行其他操作。
I have just read "Art of the Unit testing"
. And started to add unit tests. I have followed SOLID
principles as well. Especially, SRP
and DIP
, which are in my opinion prerequisites for Unit Testing.
So, most of that things which I stated above are done with specific interfaces. So, 90% job of that Command Handler is to call methods of dependencies. And 10% is the logic like this:
if(!_dependency1.IsAnySomething())
{
_dependency2.Download();
var isScriptNeeded = _dependency2.IsScriptNeeded();
if(isScriptNeeded)
{
var res = _dependency3.ExecuteScript();
_dependency4.SetScriptResult(res.Info, res.Date, res.State);
}
_dependency3.Archive();
_dependency5.DeleteTemp();
}
我已经测试了该命令处理程序的所有依赖关系。但是,帽子命令处理程序还包括一些小的逻辑,例如是否需要下载文件,是否删除临时文件等等。
我在脑海中有很多疑问,例如:
- 也许单元测试对这样的单元没有意义?集成测试可以营救吗?
- 有什么方法可以重构该类以使其可测试吗?