在单元测试期间创建文件 - 无法打开它进行写入 - TestDriven.Net 和 NUnit
我正在测试一些需要使用 FileInfo 和 DirectoryInfo 对象的代码,我认为最好在开始测试时创建一些文件,然后在测试后删除这些文件,而不是编写包装器和多个接口来解决这个问题。测试完成。这是我创建文件的方式:
public static void CreateTestSchedules(int quantity)
{
String folder = Path.Combine(Directory.GetCurrentDirectory(), "FolderFiles");
for(int quantity=10; quantity > 0; quantity--)
{
String filename = Path.GetTempFileName();
using (FileStream fileStream = File.Create(Path.Combine(folder, filename)))
{
XDocument fileContent = Helper.CreateContent(filename);
Byte[] bytes = ASCIIEncoding.ASCII.GetBytes(fileContent.ToString());
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
fileStream.Close();
}
}
}
此时,我没有看到问题:文件是在文件夹下创建的,一切看起来都很好。
然后,当测试继续执行时,我尝试打开其中一个文件以在其中写入内容,并且出现异常,表明我要打开进行写入的文件正在被其他进程使用,并且在检查后更详细地说,我将 TestDriven.Net 进程视为阻止该文件的进程。这是我用来打开并尝试将数据写入文件的代码:
using (FileStream file = new FileStream(filename, FileMode.Append))
{
Byte[] bytes = ASCIIEncoding.ASCII.GetBytes(dataToWrite.ToString());
if (file.CanWrite)
{
file.Write(bytes, 0, bytes.Length);
}
}
我的问题是:为什么会发生这种情况?没有正确释放文件句柄?有没有办法从 TestDriven.Net 窃取锁?我应该以不同的方式创建这个文件吗?我应该以其他方式编写测试吗?
预先感谢您的回答和评论=)。
编辑:
为了解决这个特定问题(真正的问题,正如 Dave Swersky 提到的那样,是单元测试不应该接触文件系统)我使用了发送的链接James Wiseman(再次感谢 James =)并使用 FileShare 标志创建了该文件,这样我就可以访问该文件,打开它并对其进行写入。像这样:
using (FileStream fileStream = new FileStream( filename, FileMode.Create, FileAccess.ReadWrite, **FileShare.ReadWrite**))
这样我就可以打开并将其写入文件。 =)
I'm testing some code that needs to use FileInfo and DirectoryInfo object and, instead of write a wrapper and several interfaces to solve this, I thought it would be a good idea create some files when starting the test and then delete those files after the test is done. This is the way I create the files:
public static void CreateTestSchedules(int quantity)
{
String folder = Path.Combine(Directory.GetCurrentDirectory(), "FolderFiles");
for(int quantity=10; quantity > 0; quantity--)
{
String filename = Path.GetTempFileName();
using (FileStream fileStream = File.Create(Path.Combine(folder, filename)))
{
XDocument fileContent = Helper.CreateContent(filename);
Byte[] bytes = ASCIIEncoding.ASCII.GetBytes(fileContent.ToString());
fileStream.Write(bytes, 0, bytes.Length);
fileStream.Flush();
fileStream.Close();
}
}
}
At this point, I don't see a problem: the files are created under the folder and everything looks fine.
Then, when the execution of the test continues, I try to open one of those file to write something in it, and I get an exception indicating the file that I want to open for writing is being used by other process and, after checking with more detail, I see the TestDriven.Net process as the one blocking the file. This is the code I use to open and try to write data to the file:
using (FileStream file = new FileStream(filename, FileMode.Append))
{
Byte[] bytes = ASCIIEncoding.ASCII.GetBytes(dataToWrite.ToString());
if (file.CanWrite)
{
file.Write(bytes, 0, bytes.Length);
}
}
My question is: why is this happening? am not releasing the file handle correctly? is there a away to steal the lock from TestDriven.Net? should I be creating this files differently? should I be writing the test in some other way?
Thanks in advance for the answers and comments =).
EDIT:
To solve THIS specific problem (the REAL problem, as Dave Swersky mentioned it, is that the Unit Test SHOULD NOT TOUCH the file system) I used the link sent by James Wiseman (thanks again James =) and created the file with a FileShare flag, that way I can reach into the file, open it an write to it. Like this:
using (FileStream fileStream = new FileStream( filename, FileMode.Create, FileAccess.ReadWrite, **FileShare.ReadWrite**))
With that I can open and write the to the file. =)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能不是您正在寻找的答案,但这正是您应该使用模拟而不是实际创建文件的原因。
这是一个用于模拟文件系统的现成解决方案: http://bugsquash .blogspot.com/2008/03/injectable-file-adapters.html
This probably isn't the answer you're looking for, but this is exactly why you should use mocks and not actually create files.
Here's a ready-made solution for mocking the file system: http://bugsquash.blogspot.com/2008/03/injectable-file-adapters.html
我注意到您直接使用
FileStream
对象来执行写入。如果您创建一个 StreamWriter 对象并将其用于文件操作(包括关闭),是否会出现同样的问题?
在谷歌上搜索发现了这篇文章。您可能会发现它很有帮助。
http://foson.blogspot.com/2007/10 /close-filestream-and-streamwriter-and.html
I notice you're using the
FileStream
object directly to perform the writing.Does the same problem present if you create a
StreamWriter
object, and use that for the file operations (including the close)?A search on Google turned up this article. You may find it helpful.
http://foson.blogspot.com/2007/10/closing-filestream-and-streamwriter-and.html