我有一个应用程序需要文件字典(文件类型和文件名列表),并将文件从原始目录复制到另一个位置。 我已经得到了复制过程的基本代码,但我需要做一些单元测试,以便尽可能健壮。Rhino Mocks测试文件系统io
我有我使用的包装类,所以我可以测试System.IO方法被调用,因为我期望,但我有一些困难,搞清楚如何形成测试,因为有foreach和switch语句在码。下面 示例代码:
private IFileSystemIO _f;
public CopyFilesToDestination(IFileSystemIO f){
_f = f;
}
public void Cpy_Files(Dictionary<string, List<string>> files)
{
// get a list of the file types in the directory
var listOfFileTypes = new List<string>(files.Keys);
foreach (var fileType in listOfFileTypes){
var fileList = files[fileType].ToList();
foreach (var file in fileList){
switch(fileType){
case ".txt":
_f.Copy(file, @"c:\destination\text");
break;
case ".dat":
_.Copy(file, @"c:\destination\data");
break;
}
}
}
}
为了测试我原以为我会用一个模拟字典对象,建立与文件类型和路径的列表,上面:
public virtual Dictionary<string, List<string>> FakeFiles(){
return fakeDictionary = new Dictionary<string, List<string>>(){
{".txt", new List<string>(){
"c:\test\file1.txt",
"c:\test\file2.txt"
}
},
{".dat", new List<string>(){
"c:\test\file1.dat",
"c:\test\file2.dat"
}
};
}
}
的第一个测试我来了看起来像这样:
[Test]
public void Should_Copy_Text_Files(){
var dictionary = new FakeDictionary().FakeFiles();
var mockObject = MockRepository.GenerateMock<IFileSystemIO>();
var systemUnderTest = new CopyFileToDestination(mockObject);
systemUnderTest.Cpy_Files(dictionary);
// I think this means "test the operation, don't check the values in the arguments" but I also think I'm wrong
mockObject.AssertWasCalled(f => f.Copy("something", "something"), o => o.IgnoreArguments());
}
我的第一个问题是:我如何测试一个特定的文件类型,如“.txt”? 那么我该如何测试循环?我用嘲讽的字典知道,我只有两个项目,我是否利用这个来形成测试?怎么样?
我觉得我可能接近一个解决方案,但我没时间/耐心地把它找下来。任何帮助是极大的赞赏。 感谢 吉姆
你如何确定在你的Switch语句中的文件类型是什么..你正在编写的包装代码看起来有点臃肿..如果你想测试一个文件是否有特定的扩展或不是为什么不做这个部分通过使用Path.GetExtension(字符串路径)传入的文件名上的switch语句采用名称为 – MethodMan 2012-01-05 13:55:11
的文件路径文件类型是专有的,实际上是文件名的一部分,实际上不是扩展名。我只是以扩展为例。但要回答您的问题,文件类型将从包含交换机的foreach中的文件类型列表中提取。 – crunchy 2012-01-06 13:30:21
好吧..这更有意义.. – MethodMan 2012-01-06 15:13:27