Я работаю над контроллером ASP.NET Core 2.1 Api, который возвращает FileStreamResult
.Мой бизнес-уровень возвращает объект MemoryStream
, который был перемотан в начальную позицию.Я пытаюсь написать модульный тест, который проверяет, является ли ожидаемое MemoryStream
возвращение из метода.Однако, когда я пытаюсь это сделать, тестовый метод просто зависает.Я использую Automapper, NSubstitute и Xunit для картографирования, макетов и тестирования соответственно.
//Action Method
[Route("Excel")]
[HttpPost]
[ProducesResponseType(typeof(string), 200)]
public ActionResult CreateExcelExport([FromBody]ExportRequestApiModel exportRequest)
{
try
{
var records = _mapper.Map<IEnumerable<ExportRecord>>(exportRequest.Records);
var result = _excelFileManager.GenerateFile(records, "sheet 1", 1);
return new FileStreamResult(result,
new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
{
FileDownloadName = "export.xlsx"
};
}
catch (Exception ex)
{
if (!(ex is BusinessException))
_logger?.LogError(LoggingEvents.GeneralException, ex, ex.Message);
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
//Action Method test.
[Fact]
public void CanCreateExportFile()
{
//Arrange
var exportRequestApiModel = new ExportRequestApiModel()
{
Records = new List<ExportRecordApiModel>() { }
};
var exportRecords = new List<ExportRecord>
{
new ExportRecord()
};
_mapper.Map<IEnumerable<ExportRecord>>(exportRequestApiModel.Records)
.Returns(exportRecords);
_excelFileManager.GenerateFile(exportRecords, "sheet 1", 1)
.Returns(new MemoryStream(){Position = 0});
//Act
var result = (ObjectResult) _controller.CreateExcelExport(exportRequestApiModel);
//Assert
Assert.Equal(StatusCodes.Status200OK, result.StatusCode);
Assert.IsType<FileStream>(result.Value);
}