Thursday, August 8, 2013

Unit Testing MVC4 Application With Dependency Injection


In this article we will see how to Unit Test a MVC4 application with Dependency Injection. 


The MVC application we will be using is already explained to a previous article. As explained in the previous article, we have a "GuestBook" controller as explained below.


One of the issues with the current implementation of the "GuestbookController" is that it directly instantiates and uses the "GuestbookContext" object, which in turn accesses the database.

The solution to this is to decouple the controller from the GuestbookContext. Instead of accessing the "GuestBook" data directly from the DB, we will create a repository as below.


A concrete class that implements the "IGuestBookRepository" interface method is created as in the following:


Note that this concrete class has the direct reference to the "DBContext". Let's integrate this repository into the "GuestBook" controller.

Before making any changes to the "GuestBook" controller, remove the DBcontext code and create a local variable to hold the "IGuestBookRepository" interface instance. 


Rather than instantiating the GuestbookContext, we now store an instance of our repository within a field. The controller's default constructor populates the field with the default implementation of the repository. The second constructor allows us to provide our own instance of the repository rather than the default. This is what we'll use in our unit tests to pass in a fake implementation of the repository.

The actions in our controller now use the repository, instead of using the LINQ queries that will do the direct DB call.


All these changes are for removing the dependency with the database and we make the existing application work without any issues.


Create a simple fake repository as below that implements the "IGuestbookRepository" interface.


Let's start to create a new unit test project from an existing application.


Create a new test method that is using our Fake repository: 


Create a complex test method like the following one, it asserts that a list of "Guestbook" objects was passed to the view.


Both tests make use of the fake repository "B". By passing it to the controller's constructor, we ensure that the controller uses our fake set of in-memory data rather than connecting to the real database.


As we saw in the Test Explorer, both tests are run successfully, independent of the database.


In this article we saw basic unit testing of MVC4 application. Dependency Injection was used in the controller to remove the database dependency. For more information about Dependency Injection, please check the following link. 

Monday, August 5, 2013

Working With MVC4 Application


In this article we will create a simple "GuestBook" application using MVC4 and Entity Framework.

First install the "EntityFramework.SqlServerCompact" NuGet package in your application.


Create a simple Model class for our application:


After this package installation, we need to create a Database context class that helps to interact with a database using our GuestBook Model.


The class inherits from the DbContext base class (that resides in the System.Data.Entity namespace), and it begins by declaring a parameterless constructor that uses constructor chaining to pass the name of the database to the base class.

We pass the string "Guestbook" to the base constructor. If we don't do this then the Entity Framework will default to using the full type-name of the context class as the name of the database, and will instead look for a file called "Guestbook.Models.GuestbookContext.sdf".

In the "App_Data" folder we can see the "GuestBook.sdf" file.


We will create a controller that we use in GuestbookContext.


We created two action methods, "index" retrieves the data from the Database and "create" is used to post data to the database.

We also created two simple views to render the Action Results.


Index view to show the "GuestBook" result from the database.


Let's see the results in a browser. The Guest Book create page looks like the following image, we simply enter the data and save that to the local database.


The result page look likes the following image:


That's it. But how is the data is stored in the database? Let's do some analysis. Install the "Glimpse" tool to begin analysis of the MVC application. Check the following link for more information about it.

As you can see, there's no connection string in Web.config, instead we will see the following data provider.


We check the Glimpse and we will get the database details as below.



In this article we saw "Guest Book" application creation in MVC4, used the Entity Framework to work with the database and used "Glimpse" to understand the Entity Framework local database.