C#模拟IFormFile CopyToAsync()方法 [英] c# mocking IFormFile CopyToAsync() method

查看:1726
本文介绍了C#模拟IFormFile CopyToAsync()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为异步功能进行单元测试,该功能将IFormFile列表转换为我自己的任意数据库文件类的列表.

I am working on a unit test for an async function that converts a list of IFormFile to a list of my own arbitrary database file classes.

将文件数据转换为字节数组的方法是:

The method that converts the file data to a byte array is:

internal async Task<List<File>> ConvertFormFilesToFiles(ICollection<IFormFile> formFiles)
{
    var file = new File
    {
        InsertDateTime = DateTime.Now,
        LastChangeDateTime = DateTime.Now
    };
    if (formFile.Length > 0)
    {
        using (var memoryStream = new MemoryStream())
        {
            await formFile.CopyToAsync(memoryStream, CancellationToken.None);
            file.FileData = memoryStream.ToArray();
        }
    }
    // ...
}

该函数接收IFormFiles的ICollection,因此它是可模拟的.

The function receives an ICollection of IFormFiles so it is mockable.

现在我有这样的测试代码:

For now I have test code like this:

//Arrange
var fileConverter = new FilesConverter();
using (var fileStream = new FileStream("Files/uploadme.txt", FileMode.Open, FileAccess.Read))
{
    using (var memoryStream = new MemoryStream())
    {
        var newMemoryStream = new MemoryStream();
        fileStream.CopyTo(memoryStream);
        FormFileMock.Setup(f => f.CopyToAsync(newMemoryStream, CancellationToken.None)).Returns(Task.CompletedTask);
        // some other setups

        //Act
        var result = await fileConverter.ConvertFormFilesToFiles(new List<IFormFile> { FormFileMock.Object });
        //Assert
        Assert.IsTrue(result.Any());
    }
}

创建newMemoryStream变量是因为该函数使用新的空内存流调用CopyToAsync方法(不确定是否有必要).

The newMemoryStream variable is created because the function calls the CopyToAsync method with a new and empty memorystream (I'm not sure if this is necessary).

问题是等待formFile.CopyToAsync(memoryStream,CancellationToken.None)不会将任何数据复制到memoryStream.

The problem is that the await formFile.CopyToAsync(memoryStream, CancellationToken.None) doesn't copy any data to the memoryStream.

推荐答案

我结合了@Nkosi和@nvoigt的答案.正如@nvoigt指出的那样:对真实文件的访问实际上不应该在单元"测试中.因此,我将文件替换为字节数组,如下所示:

I combined the answers of @Nkosi and @nvoigt. As @nvoigt pointed out: access to a real file should really not be in a "unit" test. So I replaced the file with a byte array like so:

using (var memoryStream = new MemoryStream(new byte[]{1,2,3,4}))

而不是完整的文件.

然后我按照@Nkosi的建议在模拟对象上实现了.callback

And I implemented the .callback on the mocked object as suggested by @Nkosi

FormFileMock.Setup(f => f.CopyToAsync(It.IsAny<Stream>(), CancellationToken.None))
    .Callback<Stream, CancellationToken>((stream, token) =>
    {
        // with memoryStream being the stream from inside the using statement
        memoryStream.CopyTo(stream);
    }).Returns(Task.CompletedTask);

现在可以使用了.

这篇关于C#模拟IFormFile CopyToAsync()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆