Using MVC3.NET I have a file upload method in a controller that works fine with the following signature public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> file)
How can I unit test this with NUnit? I have looked around and everyone seems to point to Moq but I’m new to unit testing and cannot get Moq working.
I have found interesting blogs such as this: http://danielglyde.blogspot.com/2011/07/tdd-with-aspnet-mvc-3-moq-and.html but am struggling to figure out how the same might be done to ‘fake’ a file upload, and am also wary that a lot on moq examples that I have managed to find now seem to have deprecated code in them.
I would simply like to know how I can simulate a HttpPostedFileBase so I can test my upload code, using Moq or otherwise – I would be really grateful if someone could give me some code examples on how to do this.
The following code taken from other examples on here:
var file = new Mock<HttpPostedFileBase>();
file.Setup(f => f.ContentLength).Returns(1);
file.Setup(f => f.FileName).Returns("test.txt");
controller.upload(file);
generates the following error when I try to compile:
cannot convert from ‘Moq.Mock’ to
‘System.Web.HttpPostedFileBase’
I have changed the method to take a singular HttpPostedFileBase for now, rather than an IEnumerable, as being able to ‘mock’ one is what I’m trying to focus on for the purpose of this question.
Assuming a standard file upload action:
you could test it like this:
Obviously all the HttpContext setup part should be externalized into a reusable class that could be called in the
[SetUp]phase of your unit test to prepare the mock context of the subject under test and to avoid repeating it in every single unit test.