Adding unit tests to your projects is one of the important thing in all the projects. Unit tests are the smallest testable parts of our application which ensures our code works as expected before we move to integrating our stuff into other modules or code. A unit test provides strict written contract that the piece of code should satisfy.
CodeRush is a Developer Productivity Tool from DevExpress. I started using it a week ago and I should say its so cool. You can download a 30 day trial version from here. I am using the same 30 day trial version now but hoping to get a licensed version in few days as I won one in a raffle happened in our local dot net user group monthly session.
Lets see how we will handle unit tests with nunit in an ASP.NET MVC project.
So I have an ASP.NET MVC project here.
To add a unit test project, simply right click on the solution and add a new class library project.
I will delete the default “Class1.cs” file because I don’t want any files in my solution which is of no use.
Now I will add a new class to this file called “ContatctControllerTest.cs”. This class will have test method to handle the ContactController in my ASP.NET MVC project.
Next is adding a reference to the project to be tested. So right click on the newly created test project and then select “Add Reference” and select the “Projects” tab.then select our MVC project and click OK button
Now we have the reference available under the “Reference” section of the test project.
Now we are going to use NUnit as our unit testing framework. So lets add the NUnit Dlls to our project. We are going to get his from the nuget repository using nuget package manager.
Right click on your test project and select “Manage Nuget Packages” from the context menu.
Search for nunit and install that.
Then I added a reference to System.Web.MVC dll in my test projects because we will be using some classes in that assembly.
Now we will set the path to nunuit dlls in coderush options window so that coderush can take care of the rest for us.
Go to Tools->DevExpress->Options and Select UnitTesting and set the path to binaries
Now we can create a class called “ContactControllerTest.cs” and put this code inside that
public void TestContactList()
ContactController c=new ContactController();
var result = c.Index() as ViewResult;
Now right click on the test method and select “RunTests”
and you can see the results in the output window
One Good think I like about code rush in this scenario is , once you run your tests, code rush will show the status of the test near to the method.
If you hover your mouse over, you will see a tool tip saying “Test has passed”. Its so awesome.