Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I unit test views and authorization attributes of my asp.net mvc application in nUnit?

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]?

like image 337
BobTheBuilder Avatar asked Nov 07 '09 14:11

BobTheBuilder


2 Answers

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.

like image 139
BenAlabaster Avatar answered Oct 13 '22 11:10

BenAlabaster


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.

like image 28
ulu Avatar answered Oct 13 '22 10:10

ulu