如何解决使用System.IO.Abstractions.TestingHelpers验证对File.Delete的调用
我使用System.IO.Abstractions.TestingHelpers来模拟FileSystem。在我的课堂上,我注入IFileSystem并使用该实例调用_fileSystem.File.Exists和_fileSystem.File.Delete。在测试类中,我想验证是否已调用“删除”方法。仅模拟IFile很容易,但是由于我已经模拟了FileSystem,所以我不想在其之上模拟目录,路径和文件。是否可以调用类似_fileRepository.FileMock.Verify(x => x.Delete(It.IsAny<string>()))
...的东西?
public class Downloader : IDownloader
{
public Downloader(HttpClient httpClient,IFileSystem fileSystem)
{
HttpClient = httpClient;
FileSystem = fileSystem;
}
public async Task DownloadConfigFileAsync(string updatedConfigBaseFolderPath,string configurationFileUrl,string personalAccesstoken)
{
var newFilePath = FileSystem.Path.Combine(updatedConfigBaseFolderPath,"subfolder1","myNewFile.txt");
if (FileSystem.File.Exists(newFilePath))
{
FileSystem.File.Delete(newFilePath);
}
// rest of implementation ommited for demo purpose
}
}
我的测试就像:
[Fact]
public async void Given_MissingPathParts_ShouldThrow()
{
var handlerMock = GetMessageHandlerMock();
var mockFileSystem = new MockFileSystem(new Dictionary<string,MockFileData>
{
{ @"c:\Test\",new MockDirectoryData() },{ @"c:\Test\subfolder1\myNewFile.txt",new MockFileData(string.Empty) }
});
var httpClient = new HttpClient(handlerMock.Object);
var sut = new Downloader(httpClient,mockFileSystem);
await sut.DownloadConfigFileAsync(BasePath,"http://fakeurl.com?path=%2Fconfiguration%2Flocal%2FTestFile.txt",_fixture.Create<string>());
handlerMock.Protected().Verify(
"SendAsync",Times.Exactly(1),ItExpr.Is<HttpRequestMessage>(req => req.Method == HttpMethod.Get),ItExpr.IsAny<CancellationToken>());
mockFileSystem.File.Exists(FilePath).Should().BeTrue();
// Add assertion that the File.Delete has been called
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。