Thursday, November 21, 2013

TDD Controller creation from unit test - Part 1

The dev team that I am currently on is making a huge effort to adopt TDD.  One discussion that comes up every now and then is what to do with your MVC controllers when it comes to TDD.  A couple of us say that you should still start with your test and a few say that it is just too easy to create a control without a test and starting from your test is just a waste of time.  This post is going to show you how easy it is to start with your test first and follow TDD the correct way using a few Resharper shortcuts.  These shortcuts are not required and there are other ways to accomplish the same thing, but Resharper does make it easier and quicker.

Do not be intimidated by the number of steps below.  I recorded the steps and it took less than two minutes to perform all steps.  You can see the recording of the steps in this video below.





1. The first thing that you need to do is create a blank solution and then add an ASP.NET MVC 4 Web Application and Unit Test Project.  If you don't know how to do this, you can read my my post about and it here.  When you are done, your solution should look similar to the following:





















2.  Add a Unit Test to your test project by right-clicking on your test project, then selecting Add -> Unit Test...










3.  Delete the provided TestMethod and use the Test Method Snippet that I introduced you to in my first blog post to give you the following test method stub.











4.  After the Assert comment, type the following:

     Assert.IsNotNull(homeController);

5.  With your cursor at the end of the line you just typed, type Alt-Enter and you should see something similar to the following:










6.  Select "Create local variable 'homeController'" by typing Enter






7.  Type the End Key















8.  Type Ctrl-X and move your cursor to just below the Arrange comment and type Ctrl-V to get your method to look like the following:












9.  Change object to var, type End, type Backspace, then type  = new HomeController();  You should now have the following:











10. With your cursor still positioned at the end of the line, type Alt-Enter and then select "Create class 'HomeController'"









11.  A class named HomeController will be created below your test method as seen  below.














12. Type End and then F6 to get the following menu:








13.  Select 'Move to Folder'.  This will open the Resharper Move to Folder dialogue as seen below.




















14.  Change the Target Folder to the Controllers folder in the ASP.NET MVC 4 Web Application that you created in step #1.  In my case, it is Namyaf.WEB\Controllers as seen below.









15.  Click Next and you will see your new file within the location you specified.













16.  Open HomeController.cs

17.  Make the HomeController class inherit from Controller

18.  Add the following using directive

     using System.Web.Mvc;











19.  Build the solution.  If you have errors, correct them.  If you cannot correct them, let me know and I can help.

20.  If you run your test within MSTEST or Resharper, you will see that the test passes.


Resharper test
MSTEST


















I hope you enjoyed this post and learned something.  In part two, I will be adding the controller actions and continuing with a little TDD.

peace yo!


2 comments:

  1. Very nice. I'm looking forward to the second part as well.

    ReplyDelete
  2. From our good friend the unit test ninja. http://www.amazon.com/gp/product/1617290890

    ReplyDelete