Mic*_*hel 10 .net c# agile unit-testing
在我读到的所有(敏捷)文章中:保持您的代码和函数小而易于测试.
我应该如何使用"控制器"或"协调员"课程?
在我的情况下,我必须导入数据.最后我有一个协调这个的对象,我想知道是否有一种方法可以保持协调员精益(呃)和意味着(呃).
我的协调员现在执行跟随(伪代码)
//Write to the log that the import has started
Log.StartImport()
//Get the data in Excel sheet format
result = new Downloader().GetExcelFile()
//Log this step
Log.LogStep(result )
//convert the data to intern objects
result = new Converter().Convertdata(result);
//Log this step
Log.LogStep(result )
//write the data
result = Repository.SaveData(result);
//Log this step
Log.LogStep(result )
Run Code Online (Sandbox Code Playgroud)
Imho,这是"知道所有"课程中的一个,或者至少有一个是"不瘦和意味着"?或者,我是否正在采取这种精益而且意味深远的事情,如果没有某种"肥胖"的进口商/协调员,是不可能对进口进行编程?
米歇尔
编辑这实际上是一个二合一的问题:一个是如何测试它,第二个是如果可以让'知道全部/粘合在一起'的协调员
Grz*_*nio 11
我相信很多人会不同意,但我认为你的方法一般都很精益.你在这里缺少的关键部分是依赖注入(也称为控制反转) - 所以不要在你的方法中新建Downloader,Converter等,你应该为这些类定义接口并在你的构造函数中"注入"它们类:
private Downloader downloader;
private Converter converter;
public MyClass(Downloader downloader, Converter converter)
{
this.downloader = downloader;
this.converter = converter;
//same with repository, it can't be a static class anymore
}
Run Code Online (Sandbox Code Playgroud)
然后你只需在你的方法中使用这些实例:
//Write to the log that the import has started
Log.StartImport()
//Get the data in Excel sheet format
result = downloader.GetExcelFile()
//Log this step
Log.LogStep(result )
//convert the data to intern objects
result = converter.Convertdata(result);
//Log this step
Log.LogStep(result )
//write the data
result = repository.SaveData(result);
//Log this step
Log.LogStep(result )
Run Code Online (Sandbox Code Playgroud)
在进行此更改之后,在测试中,您可以使用其中一个模拟框架(我使用RhinoMocks)将依赖项的模拟实现注入到您的类中.这样您就可以验证在转换器和下载器上调用了正确的方法,而无需从磁盘读取任何内容并解析任何电子表格.
如何使用RhinoMocks的示例在他们的文档中:http://ayende.com/wiki/Rhino+Mocks+Documentation.ashx
如果你遇到问题,请不要犹豫,问另一个问题:)