Thursday, March 26, 2009

Rails-like Unit Testing in MS MVC

While following the Professional ASP.NET MVC 1.0 sample chapter's section about Unit Testing, I decided to use NUnit because I do not have Visual Studio Professional. Because there is no wizard to create a Unit Test project in Visual Developer Express, I downloaded a NUnit MVC testing template and use it as my base project to complete the tutorial. The following are some different approaches I had to make in order to follow the tutorial:
  • Instead of using [TestMethod] I used [Test], which what NUnit uses to mark test methods.
  • I used the NUnit's GUI to perform the unit tests instead of using Visual Studio's since the Express version does not come with support for testing.
The tutorial in the chapter shows you how to create a fake repository so that your unit tests do not use the database. Because I come from a Rails background where you actually have (or supposed to have) a separated database for testing, I decided to just copy the database and use the new copy for testing. The biggest obstacle was to make my C# test project use a provided connection string instead of using the assembly's default which uses System.Configuration.ConfigurationManager.ConnectionStrings["NerdDinnerConnectionString"].ConnectionString . After changing the controller and the repository to have constructors that allow passing the connection string, I was able to test using my Test database. You can download the final project here.

0 comments: