我只是不确定如何模拟情况来测试这个。我应该在文件系统上实际创建文件吗?
public static void DeleteIfExists(this FileInfo fileInfo)
{
if (fileInfo.Exists)
{
fileInfo.Delete();
}
}
答案 0 :(得分:8)
我使用了一个模拟框架,比如RhinoMocks。
[Test]
public void ShouldDeleteAFileWhenItExists()
{
var mockInfo = MockRepository.GenerateMock<FileInfo>();
mockInfo.Expect( i => i.Exists ).Return( true ).Repeat.Once();
mockInfo.Expect( i => i.Delete() ).Repeat.Once();
var extensions = new FileInfoExtensions();
extensions.DeleteIfExists( mockInfo );
mockInfo.VerifyAllExpectations();
}
[Test]
public void ShouldNotDeleteAFileWhenItDoesNotExist()
{
var mockInfo = MockRepository.GenerateMock<FileInfo>();
mockInfo.Expect( i => i.Exists ).Return( false ).Repeat.Once();
mockInfo.Expect( i => i.Delete() ).Repeat.Never();
var extensions = new FileInfoExtensions();
extensions.DeleteIfExists( mockInfo );
mockInfo.VerifyAllExpectations();
}
Delete删除异常等的其他测试