Я хочу написать интеграционный тест для моего контроллера, который возвращает HttpResponseMessage, в котором «Содержимое» является строкой, а его
тип содержимого "application / vnd.openxmlformats-officedocument.spreadsheetml.sheet"
Как мне преобразовать эту строку обратно в экземпляр ExcelFile - libarary gembox?
этот контроллер например:
/// <summary>
/// Save Data
/// </summary>
/// <param name="request"></param>
/// <returns></returns>
[HttpPost]
[Route(Routes.Download)]
[ValidateToken]
[SwaggerResponse(HttpStatusCode.OK, "Xlsx-file with result", typeof(HttpStatusCode))]
[SwaggerResponse(HttpStatusCode.Unauthorized, "invalid token", typeof(HttpStatusCode))]
[SwaggerResponse(HttpStatusCode.BadRequest, "Message with description error of validate", typeof(HttpStatusCode))]
public HttpResponseMessage PostUnloadExcelFile([FromBody]DownloadRequest request)
{
var logData = _logMessageBuilder.GetLogMessageDownload(request.Request);
LogRequest(logData);
var response = Request.CreateResponse(HttpStatusCode.OK);
_downloadService.GetExcelFile(request.Data).Save(response, $"{Routes.Download}.xlsx");
return response;
}
этот пример DownloadService
public class DownloadService : BaseDownloadService, IDownloadService
{
private const string Sheet = "Work sheet";
public ExcelFile GetExcelFile(IList<Event> data)
{
var xlsFile = new ExcelFile();
var workSheet = xlsFile.Worksheets.Add(Sheet);
SetHeaderWorksSheet(workSheet);
for (var i = 0; i < data.Count; i++)
{
SetCellWorksSheet(data[i], workSheet, i);
}
for (var j = 0; j < data.Count + 1; j++)
{
SetCellStyleWorksSheet(workSheet, j);
}
return xlsFile;
}
}
public class BaseDownloadService
{
protected void SetHeaderWorksSheet(ExcelWorksheet workSheet)
{
workSheet.Cells[0, ExcelFileIndexes.Item].Value = ConstantsExcelHeader.Item;
workSheet.Cells[0, ExcelFileIndexes.FirstDate].Value = ConstantsExcelHeader.FirstDate;
workSheet.Cells[0, ExcelFileIndexes.LastDate].Value = ConstantsExcelHeader.LastDate;
}
protected void SetCellWorksSheet(Triplet rowData, ExcelWorksheet workSheet, int i)
{
workSheet.Cells[i + 1, ExcelFileIndexes.Item].Value = (i + 1).ToString();
workSheet.Cells[0, ExcelFileIndexes.FirstDate].Value = rowData.FirstDate;
workSheet.Cells[0, ExcelFileIndexes.LastDate].Value = rowData.LastDate;
}
protected void SetCellStyleWorksSheet(ExcelWorksheet workSheet, int i)
{
workSheet.Columns[i].AutoFit();
workSheet.Cells[0, i].Style
.FillPattern
.SetPattern(
FillPatternStyle.Solid,
SpreadsheetColor.FromName(ColorName.LightGreen),
SpreadsheetColor.FromName(ColorName.LightGreen));
}
}
этот тест, например:
[Theory]
[MemberData(nameof(DataForTrySaveInExcelFileTesting))]
public async Task Should_ReturnCode200TrySaveDataInFileExcel(DownloadRequest request)
{
// Arrange
var token = await GetValidTokenAsync();
// Act
var response = await ActAsync<DownloadRequest, DataRecord>(request, token, DownloadUrl);
var excelBytes = Encoding.UTF8.GetBytes(response.Content);
var excelFile = new ExcelFile();
using (MemoryStream stream = new MemoryStream())
{
stream.Write(excelBytes, 0, excelBytes.Length);
stream.Position = 0;
excelFile = ExcelFile.Load(stream, LoadOptions.XlsxDefault);
}
var ws = excelFile.Worksheets.ActiveWorksheet;
ws.Cells["B2"].Value.ShouldBeEqualTo("1234567890");
}
Это ответ для фронта