The year 2012 was the year of hypes and we had lot of discussions about Cloud, Big Data and Enterprise Mobility. The year 2013 will be the year, where we can see lot of hypes will become realities. Just going behind the hypes will not help you, but a pragmatic approach to new technologies will give you lot of business values and operational agility. In 2013, Mobile apps will continue the momentum and Cloud adoption will be widely improved on 2013. Organizations, especially larger organizations will invest huge amount for enabling the enterprise mobility in the year 2013, and they will try to expose REST/Hypermedia APIs over the organizational data, for providing a better mobility across the enterprise and public world.
In this blog post, I will demonstrate how to write unit tests for an ASP.NET MVC action method, which handles both Ajax request and normal HTTP Request. I will write a unit test for specifying the behavior of an Ajax request and will write another unit test for specifying the behavior of a normal HTTP request. Both Ajax request and normal request will be handled by a single action method. So the ASP.NET MVC action method will be execute HTTP Request object’s IsAjaxRequest method for identifying whether it is an Ajax request or not. So we have to create mock object for Request object and also have to make as a Ajax request from the unit test for verifying the behavior of an Ajax request. I have used NUnit and Moq for writing unit tests.
I have just migrated my EFMVC app from .NET 4.0 and ASP.NET MVC 4 RC to .NET 4.5, ASP.NET MVC 4 RTM and Entity Framework 5 Code First. In this release, the EFMVC solution is built with Visual Studio 2012 RTM. The migration process was very smooth and did not made any major changes other than adding simple unit tests with NUnit and Moq. I will add more unit tests on later and will also modify the existing solution.