简体   繁体   中英

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 [ie unit testing that the Authorize attribute is functioning correctly]?

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM