写一个文件是不可能的。基本上,这是情况。我有以下几点:C#(单元测试):我需要通过代码
public IService Service { get; set; } //Set to MyMockedService class.
public Boolean DoFoo()
{
//possible other ways of returning true/false...
Boolean success = true;
//Get FileInfo[] items
foreach (var item in items)
DoOtherFoo(item);
}
public Boolean DoOtherFoo(FileInfo fileInfo)
{
String filepath = //manipulate fileInfo.FullName;
Byte[] file = Service.GetFile(filepath)
try
{
WriteBinaryFile(filepath, file); //How can I force file writing to throw an exception
}
catch (Exception)
{
return false;
}
}
基本上,在测试DoFoo()我有很多路径可能会返回true/false。我已经测试过除最后一个之外的所有这些文件......它试图写入文件,并且即使其中一个文件由于某种原因而无法写入,它也会失败并返回false。起初,我想如果我试图设置一个不好的文件名,例如“bad * file”,它会在WriteFile中抛出一个异常,但我甚至没有那么远,因为我无法使用非法字符创建一个FileInfo对象。所以我正在寻找另一种方式来制作它,这样就不可能编写这个文件,这样我就可以找回一个错误的文件。
为什么不在运行应用程序时才打开文件。取决于它是什么文件,目前的应用程序可能会锁定它,因此您将无法写入它。 – melaos 2011-01-12 16:21:01
你有没有看到我的答案? – 2011-01-12 17:41:23