在控制器中,我有一个返回FileStreamResult
对象的操作结果,在此之前,该操作使用File
类的byte[] ReadAllBytes(string path)
。
行动结果是:
public async Task<IActionResult> Download(string path)
{
var myfile = System.IO.File.ReadAllBytes(path);
MemoryStream stream = new MemoryStream(myfile);
return new FileStreamResult(stream, "application/pdf");
}
在我的xUnit测试项目中,我使用Moq进行设置。
模拟:
using IFileSystem = System.IO.Abstractions.IFileSystem;
private readonly Mock<IFileSystem> _fileSystem = new Mock<IFileSystem>();
试验方法:
[Fact]
public async Task Download_ShouldReturnPdfAsFileStreamResult_WhenIsFoundByPath()
{
//Arrange
var expected = new byte[]
{
68, 101, 109, 111, 32, 116, 101, 120, 116, 32, 99, 111, 110, 116,
101, 110, 255, 253, 0, 43, 0, 32, 0, 115, 0, 111, 0, 109, 0, 101,
0, 32, 0, 116, 0, 101, 0, 120, 0, 116
};
var path = _fixture.Create<string>();
_fileSystem.Setup(f => f.File.ReadAllBytes(It.IsAny<string>()))
.Returns(expected);
//Act
var result = await _sutController.Download(path )
.ConfigureAwait(false) as FileStreamResult;
//Assert
result.Should().NotBeNull();
//...
}
现在,当我运行测试时,我会得到这个异常:
留言:
System.IO.FileNotFoundException : Could not find file 'C:\Users\Admin\Desktop\GF\Tests\GF.Web.Controllers.Tests\bin\Debug\net6.0\Path69bdc5aa-695a-4779-b38e-12cb2df4c21a'.
Stack Trace:
SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
OSFileStreamStrategy.ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize)
FileStreamHelpers.ChooseStrategy(FileStream fileStream, String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, Int64 preallocationSize)
File.ReadAllBytes(String path)
2条答案
按热度按时间ctehm74n1#
Alexandria 在评论中建议的更合理的方法是这样的:
首先开始在web和单元测试项目中安装nuget包https://www.nuget.org/packages/System.IO.Abstractions/:
然后将IFileSystem插入控制器
当然,在启动时配置依赖项注入
现在在控制器操作结果中使用
_fileSystem.File.ReadAllBytes(path)
代替System.IO.File.ReadAllBytes(path)
现在,在测试类中,只需在构造器中注入IFileSystemMock
并设置**_文件系统.文件.读取所有字节**:
应为
new byte[]
imzjd6km2#
有一个替代的纯粹的方式来模拟(Moq),不添加任何Nuget包。实现一个接口和类。然后依赖注入接口。现在你可以在测试类中模拟。