【发布时间】:2016-02-27 15:51:49
【问题描述】:
这是我要测试的方法之一中的代码:
using (var sr = new StreamReader(myFile))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Equals("completed"))
{
continue; //here it is getting called infinite times
}
if(line.Equals("processed"))
{
break;
}
}
}
在我的测试方法中,我在 shim 的帮助下编写了以下代码:
ShimStreamReader.ConstructorString = delegate (StreamReader @this, string @string)
{
var reader = new ShimStreamReader(@this);
reader.ReadLine = () =>
{
return "completed";
};
};
现在我不想传递文件。相反,我想传递字符流或字符串。 上面的测试代码按预期调用并打破了 new StreamReader(myFile) 的依赖关系并进入了 while 循环。 一旦在 while 循环中输入它, sr.ReadLine() 就会一直返回“已完成”。所以我被困在这里。我将如何在这里停下来,或者我将如何编写输入字符串,以便我的第一次调用返回在第二次调用 sr.ReadLine() 时完成,它应该返回 null,然后中断循环?
【问题讨论】:
标签: unit-testing readline microsoft-fakes