问题描述
我使用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 (将#修改为@)