Click here to Skip to main content
14,836,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Wondering if anyone can help!

I am trying to create a unit test with Mocks a IFormFIle Object as with the below code

[Fact]
public void HasCorrectImageType()
{
    var formFile =  new Mock<IFormFile>();
    var PhysicalFile = new FileInfo(@"./Images/Dart 1.jpg");
    var memory = new MemoryStream();
    var writer = new StreamWriter(memory);
    writer.Write(PhysicalFile.OpenRead());
    writer.Flush();
    memory.Position = 0;
    var fileName = PhysicalFile.Name;

    formFile.Setup(_ => _.FileName).Returns(fileName);
    formFile.Setup(_ => _.Length).Returns(memory.Length);
    formFile.Setup(_ => _.OpenReadStream()).Returns(memory);
    formFile.Verify();

    var controller = new ScanController();
    var file = formFile.Object;

    var result = controller.Scan(file);
    Assert.IsAssignableFrom<OkObjectResult>(result);
}


Now when I test this it gets to my controller with no issue, BUT I need to test to ensure that the file is "image/jpg" by using the ContentType property in the IFormFile parameter in my controller method as I want to then pass it to a OCR Framework.

[HttpPost]
[ActionName("Scan File")]
[ProducesResponseType(200)]
public IActionResult Scan(IFormFile fileInfo)
{
    if (fileInfo == null) return BadRequest();
    if (fileInfo.ContentType == "image/jpg") return BadRequest();

    return Ok();
}


When stepping through this code the ContentType property always returns null when called through XUnit tests.

What I have tried:

If I try it in Postman or Fiddler the call always works correctly to the current code base.

I have tried to set the property to "image/jpg" but the property is only a getter

Now I am stuck / scratching my head.
Posted
Updated 19-Jan-19 10:36am
Comments
Bohdan Stupak 13-Jan-19 10:11am
   
I definitely don't have an answer to your question but I'm wondering whether there is any chance that you could write integration level test with just directly uploading different files, checking response codes and cleaning up instead of unit testing this functionality.
Anyways, have a nice day.
Simon_Whale 13-Jan-19 10:17am
   
Good thought, thanks I will look into mocking (excuse the choice of words) this up instead

1 solution

I would like to update this so that someone else can have the answer if needed.

I had to change the process of the test from a unit test to an integration test, I have also included the code below

using (var stream = File.OpenRead(@"./Images/Dart 1.jpg"))
{
  var file = new FormFile(stream, 0, stream.Length, null, Path.GetFileName(@"./Images/Dart 1.jpg"))
  {
    Headers = new HeaderDictionary(),
    ContentType = "image/jpeg"
  };

  var controller = new ScanController();
  var result = controller.Scan(file);
  Assert.IsAssignableFrom<OkResult>(result);
}
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900