I have an api that receives an xlsx file and read it, it works fine wiht the real file, but when i mock to test it does not.
I have this api test that creates an IFomFile
public IFormFile CreateCorrectClientByFileMock() {
var fileName = "fileName.xlsx";
var stream = new MemoryStream();
var writer = new StreamWriter(stream);
writer.WriteLine("name;email;date");
writer.Flush();
stream.Position = 0;
IFormFile file = new FormFile(stream, 0, stream.Length, "id_from_form", fileName)
{
Headers = new HeaderDictionary(),
ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
};
stream.Close();
return file;
}`
and send it to this function:
public List<Client> ReadFile(IFormFile file){
using var stream = file.OpenReadStream();
XLWorkbook wb = new(stream);
and does all the processes to read......
}
it trowns an "System.IO.FileFormatException:'File contains corrupted data.'" at line "XLWorkbook wb = new(stream);"
When i call the function with a real file it works fine, but when I try To create the IFormFile for test it does not.