I am writing an ASP.net MVC 4 application, and I am thinking about using "in memory" hosting to write integration tests for my custom action filters.
There are a few examples on the web on how to do this with Web API (eg http://www.strathweb.com/2012/06/asp-net-web-api-integration-testing-with-in-memory-hosting/) but I haven't seen any example with MVC.
Is it possible to do "in memory" hosting with MVC applications? If so does anyone have any examples or could they point me to any articles that do this?
ASP.NET Web API can be either be hosted in IIS or in a separate host process. The former approach is usually appropriate when the Web API is part of a web application and one or more web applications are going to consume it.
Web API can be used for generating HTTP services that replies data alone, but MVC would be suitable for developing web applications that replies as both, views and data. Web API looks at Accept Header of the request who returning the data in various formats, so it can return in various formats, like XML, JSON etc.
Key ASP.NET MVC Hosting features Separation of tasks. With ASP.NET MVC, you can build web applications as a composite of three different roles. The "Model" is the application state typically stored in a database. The "View" extracts information from the "Model" and displays the information.
Sdk package. Entity Framework Core is also used in the tests. The app references: Microsoft.
You might be interested in MvcIntegrationTestFramework. It is not the freshest one but I works. Here is self-explaining example from their page:
AppHost.Simulate("MyMvcApp").Start(browsingSession =>
{
var loginResult = browsingSession.Post("Users/Login/",
new { UserName = "aaa", Password = "bbb" });
Assert.That(loginResult.Response.StatusCode, Is.EqualTo(200));
var result = browsingSession.Post("Money/Create/",
new { Amount = "1,000,000" });
Assert.That(result.Response.StatusCode, Is.EqualTo(200));
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With