views:

201

answers:

3

I'm at a point where I'd like to start writing unit tests for my MVC application. I've already figured out how to unit test the controller and I can unit test my underlying business libraries without any problem. I'm coming unstuck on a couple of items though:

  • How do I unit test my views? That is, after a controller has returned the correct view (which I've unit tested, along with unit testing to check it's passing the correct data). Is there a way to unit test that the view is displaying the correct data in the correct fields in nUnit or do I need a different tool for this purpose?

  • How do I unit test the security? I'm using a custom membership provider and role provider which in themselves I guess is unit testable like any other component - I just new up the provider and check that each of the methods returns expected output. What I'm curious about is how do I unit test that each of my controller methods is receiving the correct security information from my providers [i.e. unit testing that the Authorize attribute is functioning correctly]?

A: 

For integration testing below the UI (Controllers and down) you can use something like Fit, FitNesse, Slim, or even StoryTeller.

For UI testing (your Views) you should consider using Selenium, and Selenium RC.

I realize this is a lot of information, but it will suit you well when it comes to a more robust testing solution.

Josh
For the controllers and down, I tend to use nUnit - I'm already familiar with unit testing on a library level so that's not an issue. I'll admit I've never heard of Slim or StoryTeller so I'll check them out. I will check out Selenium for the view testing though.
BobTheBuilder
Selenium RC certainly looks interesting. It didn't occur to me that the unit testing would be done in the browser itself. My original perception was that the unit testing would run in nUnit on the HTTP output.
BobTheBuilder
You want clean separation of concerns even in your unit testing. I typically don't let my unit tests (Nunit, MsTest, etc..) touch the database or anything other than the current subject under test. Fit, FitNesse, Slim and StoryTeller are great for integration testing, but should not be used for unit testing. Testing the UI should be very shallow, and should only test that the UI does what it is supposed to. It should not under any circumstances be checking stuff under the hood (i.e. database, controllers, etc...)
Josh
+1  A: 

You can use Ivonna, a commercial tool, or MvcIntegrationTestFramework, a free one. Both support testing your views in-process, but Ivonna lets you mock the rest, tweak your config etc., so it's more on the unit testing side.

ulu
+1  A: 

Here's a blog post on unit testing custom authorization components. It doesn't address RoleProviders and MembershipProviders, but you should be able to effectively use the same method for testing any number of filters.

http://darioquintana.com.ar/blogging/2009/05/23/aspnet-mvc-testing-a-custom-authorize-filters/

Disclaimer: It might take a couple of parses to understand it - it's not very clearly written and the example code provided doesn't exactly cut to the chase either, but you should be able to glean what you need from it.

BenAlabaster