Unit Test a file upload, how? - asp.net-mvc-3

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:
[HttpPost]
public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> files)
{
foreach (var file in files)
{
var filename = Path.Combine(Server.MapPath("~/app_data"), file.FileName);
file.SaveAs(filename);
}
return View();
}
you could test it like this:
[Test]
public void Upload_Action_Should_Store_Files_In_The_App_Data_Folder()
{
// arrange
var httpContextMock = new Mock<HttpContextBase>();
var serverMock = new Mock<HttpServerUtilityBase>();
serverMock.Setup(x => x.MapPath("~/app_data")).Returns(#"c:\work\app_data");
httpContextMock.Setup(x => x.Server).Returns(serverMock.Object);
var sut = new HomeController();
sut.ControllerContext = new ControllerContext(httpContextMock.Object, new RouteData(), sut);
var file1Mock = new Mock<HttpPostedFileBase>();
file1Mock.Setup(x => x.FileName).Returns("file1.pdf");
var file2Mock = new Mock<HttpPostedFileBase>();
file2Mock.Setup(x => x.FileName).Returns("file2.doc");
var files = new[] { file1Mock.Object, file2Mock.Object };
// act
var actual = sut.UploadFile(files);
// assert
file1Mock.Verify(x => x.SaveAs(#"c:\work\app_data\file1.pdf"));
file2Mock.Verify(x => x.SaveAs(#"c:\work\app_data\file2.doc"));
}
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.

Related

Use MVC's model binding from application code

As a sample of what I'm trying to accomplish, here in MapPost I'm manually parsing the body of the HTTP request.
// Program.cs
using System.Text.Json;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
Type[] types = new[] { typeof(SampleDto1), typeof(SampleDto2), <other unknown types> };
foreach (var type in types)
{
app.MapPost(type.Name, async (HttpContext httpContext) =>
{
var request = await JsonSerializer.DeserializeAsync(
httpContext.Request.Body,
type,
new JsonSerializerOptions(JsonSerializerDefaults.Web),
httpContext.RequestAborted);
return Results.Ok(request);
});
}
app.Run();
internal record SampleDto1(string Input) { }
internal record SampleDto2(string Input) { }
This works, yay! However, ... ASP.NET Core's MVC has all these sophisticated ModelBinding functionality and I really would like to use that. Because that opens up possibilities for binding to querystring parameters and other sources instead of only the request body.
Basically I want to replace the call to JsonSerializer with a call to framework code.
I've been browsing the ASP.NET Core source code and at first the DefaultModelBindingContext looked promising. However, I soon stumbled on some internal classes which I couldn't access from my code.
Long story short, .. is it at all possible to plug-in to MVC's model binding from application code?
Update: Although it doesn't show from the initial question, the solution should work dynamically with any request type. Not only SampleDto1 and SampleDto2. That's why explicit parameter binding from Minimal API won't do the trick.
You could try the codes :
using Microsoft.AspNetCore.Mvc;
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
builder.Services.AddSingleton<Service>();
app.MapPost("/{id}", ([FromRoute] int id,
[FromQuery(Name = "p")] int page,
[FromBody]SampleDto1 sample1,
[FromBody] SampleDto2 sample2,
[FromServices] Service service,
[FromHeader(Name = "Content-Type")] string contentType)
=> { });
app.Run();
internal record SampleDto1(string Input) { }
internal record SampleDto2(string Input) { }
You could read the official document for more details:
https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis?view=aspnetcore-6.0#explicit-parameter-binding

How to mock httpcontext so that it is not null from a unit test?

I am writing a unit test and the controller method is throwing an exception because HttpContext / ControllerContext is null. I don't need to assert anything from the HttpContext, just need it to be not NULL. I have done research and I believe Moq is the answer. But all the samples that I have seen haven't helped me a lot. I don't need to do anything fancy, just to mock the httpcontext. Point me in the right direction!
Got these two functions from here in a class;
public static class HttpContextFactory
{
public static void SetFakeAuthenticatedControllerContext(this Controller controller)
{
var httpContext = FakeAuthenticatedHttpContext();
ControllerContext context =
new ControllerContext(
new RequestContext(httpContext,
new RouteData()), controller);
controller.ControllerContext = context;
}
private static HttpContextBase FakeAuthenticatedHttpContext()
{
var context = new Mock<HttpContextBase>();
var request = new Mock<HttpRequestBase>();
var response = new Mock<HttpResponseBase>();
var session = new Mock<HttpSessionStateBase>();
var server = new Mock<HttpServerUtilityBase>();
var user = new Mock<IPrincipal>();
var identity = new Mock<IIdentity>();
context.Setup(ctx => ctx.Request).Returns(request.Object);
context.Setup(ctx => ctx.Response).Returns(response.Object);
context.Setup(ctx => ctx.Session).Returns(session.Object);
context.Setup(ctx => ctx.Server).Returns(server.Object);
context.Setup(ctx => ctx.User).Returns(user.Object);
user.Setup(ctx => ctx.Identity).Returns(identity.Object);
identity.Setup(id => id.IsAuthenticated).Returns(true);
identity.Setup(id => id.Name).Returns("a.ali174");
return context.Object;
}
}
From the unit test I called them as such;
HttpContextFactory.SetFakeAuthenticatedControllerContext(controller);
Make sure you have Moq installed in your tests project:
Install-Package Moq
I know this is an older subject, however Mocking a MVC application for unit tests is something we do on very regular basis.
I just wanted to add my experiences Mocking a MVC 3 application using Moq 4 after upgrading to Visual Studio 2013. None of the unit tests were working in debug mode and the HttpContext was showing "could not evaluate expression" when trying to peek at the variables.
Turns out visual studio 2013 has issues evaluating some objects. To get debugging mocked web applications working again, I had to check the "Use Managed Compatibility Mode" in Tools=>Options=>Debugging=>General settings.

MVC3 unit testing response code

I have a controller within MVC3 which needs to return a response code 500 if something goes wrong. I am doing this by returning a view object and setting http response code to equal 500 (I have checked this in firebug and all is working great).
public ActionResult http500()
{
ControllerContext.HttpContext.Response.StatusCode = 500;
ControllerContext.HttpContext.Response.StatusDescription = "An error occurred whilst processing your request.";
return View();
}
The problem I have now is I need to be able to write a unit test which checks the response code. I have tried accessing the response code in several different ways both through the ViewResult object and the Controller context.
Neither way gives me the response code I have set in the controller.
[TestMethod()]
public void http500Test()
{
var controller = new ErrorController();
controller.ControllerContext = new ControllerContext(FakeHttpObject(), new RouteData(), controller);
ViewResult actual = controller.http500() as ViewResult;
Assert.AreEqual(controller.ControllerContext.HttpContext.Response.StatusCode, 500);
}
How would I go about getting the response code 500 from the controller or is this more of an integration testing thing.
How about doing it in a more MVCish way:
public ActionResult Http500()
{
return new HttpStatusCodeResult(500, "An error occurred whilst processing your request.");
}
and then:
// arrange
var sut = new HomeController();
// act
var actual = sut.Http500();
// assert
Assert.IsInstanceOfType(actual, typeof(HttpStatusCodeResult));
var httpResult = actual as HttpStatusCodeResult;
Assert.AreEqual(500, httpResult.StatusCode);
Assert.AreEqual("An error occurred whilst processing your request.", httpResult.StatusDescription);
or if you insist on using the Response object you could create a fake one:
// arrange
var sut = new HomeController();
var request = new HttpRequest("", "http://example.com/", "");
var response = new HttpResponse(TextWriter.Null);
var httpContext = new HttpContextWrapper(new HttpContext(request, response));
sut.ControllerContext = new ControllerContext(httpContext, new RouteData(), sut);
// act
var actual = sut.Http500();
// assert
Assert.AreEqual(500, response.StatusCode);
Assert.AreEqual("An error occurred whilst processing your request.", response.StatusDescription);
What is FakeHttpObject()? Is it a mock created using Moq? In that case you need to setup setters and getters to store the actual values somewhere. Mock<T>doesn't provide any implementation for properties and methods. When setting a value of property literally nothing happens and the value is 'lost'.
Another option is to provide a fake context that is a concrete class with real properties.

FindItems() and BindToItems() give inconsistent results for EmailMessage.Sender.Address

After quite a lot of debugging, I've refined a complicated Managed EWS problem down to the following two simple-ish test cases. The first one works, the second one fails:
var view = new ItemView(100) { PropertySet = new PropertySet { EmailMessageSchema.Id } };
var findResults = ews.FindItems(WellKnownFolderName.Inbox, view)
var bindResults = ews.BindToItems(findResults.Select(r => r.Id), new PropertySet { EmailMessageSchema.Sender });
// Sanity check
Assert.AreEqual(1, bindResults.Count());
// The results I care about
Assert.AreEqual("David Seiler", bindResults[0].Sender.Name);
Assert.AreEqual("david.seiler#yahoo.com", bindResults[0].Sender.Address);
One might try to cut out the BindToItems() call, and use FindItems() directly:
var view = new ItemView(100) { PropertySet = new PropertySet { EmailMessageSchema.Sender } };
var findResults = ews.FindItems(WellKnownFolderName.Inbox, view)
// This part still works fine
Assert.AreEqual(1, findResults.Count());
// So does this
Assert.AreEqual("David Seiler", findResults[0].Sender.Name);
// ...but this fails! Sender.Address is null
Assert.AreEqual("david.seiler#yahoo.com", findResults[0].Sender.Address);
Can anyone tell me where I've gone wrong? It really seems, from the documentation, as though this should work. Not all properties can be read through FindItems(), it's true, but those properties usually throw when I try to access them, and anyway there's a list of those properties on MSDN and Sender isn't on it. What's going on?
Actually I don't know why, but in the second option, it only load basic information of the sender like the name, but not the Address.
If you want to load all the sender properties but do not want to bind the full message you can add the following line before the first assert
service.LoadPropertiesForItems(findResults.Items, new PropertySet(EmailMessageSchema.Sender));

Add ViewFolder in config for Spark in ASP.NET MVC Unit Test project

I'm sending an email from my ASP.NET MVC app using the Spark View Engine based on this example by Andrew Kharlamov.
I've setup a unit test, CanSendEmail, but I need to specify the viewfolder in the config.
I found the documentation here and the examples give this:
<spark>
<views>
<add name="{any-unique-name}"
folderType="FileSystem|EmbeddedResource|VirtualPathProvider|Custom"
type="{name, assembly of IViewFolder type}"
constuctor-param-names="values"
subfolder="{optional subfolder to target}"/>
</views>
</spark>
My question is this. Which folderType do I use and do I need any other parameters. My test product is call myProject.Tests and my web project containing the views is called myProject.Web with a Views folder in it.
Do I use FileSystem, VirtualPathProvider ... ?
Edit [14/11/2011]:
Okay I've got this in my app.config in myProject.Tests:
<views>
<add name="web-view-folder"
folderType="VirtualPathProvider"
virtualBaseDir="~/Views"/>
</views>
I still get "View source file not found." when I run my test. I want the test to use the Views in myproject.Web.
My Solution
Based on the blog posts here and here, and with help from #RobertTheGrey and looking at the tests in the Spark source code, I ended up using ViewFolderType.FileSystem. That worked.
Here's the my code under test:
public string RenderEmailWithCustomViewFolder(string sparkViewName, ViewDataDictionary viewData, Dictionary<string, string> viewFolderParameters)
{
var settings = new SparkSettings()
.SetPageBaseType(typeof (SparkView))
.AddViewFolder(ViewFolderType.FileSystem, viewFolderParameters)
.AddAssembly("MvcContrib");
var engine = new SparkViewEngine(settings);
var sparkViewDescriptor = new SparkViewDescriptor().AddTemplate(sparkViewName);
var view = (SparkView)engine.CreateInstance(sparkViewDescriptor);
try
{
// Merge view data
viewData.Keys.ToList().ForEach(x => view.ViewData[x] = viewData[x]);
// Render the view to a text writer
var writer = new StringWriter();
view.RenderView(writer);
return writer.ToString();
}
finally
{
engine.ReleaseInstance(view);
}
}
And here's my test:
[Test]
public void Can_Render_Order_Confirmation_Email_With_Spark_View_Engine()
{
// Arrange
var order = OrderInstanceFactory.CreateTestOrder();
order.ContactEmail = "test#testicle.com";
var emailService = new EmailService();
var viewData = new ViewDataDictionary();
viewData["Order"] = order;
const string viewFolder = #"../../../../app/myProject.Web/Views";
var viewFolderParameters = new Dictionary<string, string> {{"basePath", viewFolder}};
// Act
var emailBody = emailService.RenderEmailWithCustomViewFolder("Email/OrderConfirmation.spark", viewData, viewFolderParameters);
// Assert
Assert.IsNotNull(emailBody);
Assert.IsTrue(emailBody.Contains("test#testicle.com"));
}
My OrderConfirmation.spark template lives in my web products in the Views/Email/.
If it's an ASP.NET MVC app, then you can use VirtualPathProvider since that hooks into the HttpContext and the rest of the runtime. You would use a FileSystemProvider if you were runnig it from a console app for example, or if you wanted to add a folder from outside your web app, perhaps because the templates were shared by other apps, but I've rarely seen that done.
Hope that helps...

Resources