问题
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();
}
}
// ...
}
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());
}
}
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).
The problem is that the await formFile.CopyToAsync(memoryStream, CancellationToken.None) doesn't copy any data to the memoryStream.
回答1:
I know this might be unpopular because using a mock framework is totally "in" these days, but why not simply leave the framework be framework and go the simple, easy way? You can create a FormFile
without any mocking. Just the real deal:
var fileConverter = new FilesConverter(FilesConverterLoggerMock.Object, FileDataMock.Object);
// access to a real file should really not be in a "unit" test, but anyway:
using (var stream = new MemoryStream(File.ReadAllBytes("Files/uploadme.txt"))
{
// create a REAL form file
var formFile = new FormFile(stream , 0, stream.Length, "name", "filename");
//Act
var result = await fileConverter.ConvertFormFilesToFiles(new List<IFormFile> { formFile });
//Assert
Assert.IsTrue(result.Any());
}
回答2:
The problem is that the
await formFile.CopyToAsync(memoryStream, CancellationToken.None)
doesn't copy any data to thememoryStream
.
According to your setup. Nothing will actually be copied.
You just setup the call to return as completed. No actual functionality was implemented.
You'll need to add a Callback
to perform some desired functionality before returning the task.
FormFileMock
.Setup(_ => _.CopyToAsync(It.IsAny<Stream>(), CancellationToken.None))
.Callback<Stream, CancellationToken>((stream, token) => {
//memory stream in this scope is the one that was populated
//when you called **fileStream.CopyTo(memoryStream);** in the test
memoryStream.CopyTo(stream);
})
.Returns(Task.CompletedTask);
回答3:
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}))
Instead of a complete file.
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);
And now it works.
来源:https://stackoverflow.com/questions/47309040/c-sharp-mocking-iformfile-copytoasync-method