如何在nUnit中对我的asp.net mvc应用程序的视图和授权属性进行单元测试?

时间:2022-10-21 04:13:56

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:

我正处于开始为我的MVC应用程序编写单元测试的地步。我已经想出了如何对控制器进行单元测试,我可以毫无问题地对我的底层业务库进行单元测试。我现在要解开几件物品:

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

    如何对我的观点进行单元测试?也就是说,在控制器返回正确的视图(我已经过单元测试,以及单元测试以检查它是否传递了正确的数据)之后。有没有办法单元测试视图在nUnit中的正确字段中显示正确的数据,还是我需要一个不同的工具用于此目的?

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

    我如何对安全性进行单元测试?我正在使用自定义成员资格提供程序和角色提供程序,它本身就像任何其他组件一样可以进行单元测试 - 我只是新建了提供程序并检查每个方法是否返回预期输出。我很好奇的是如何对我的每个控制器方法从我的提供商那里接收正确的安全信息进行单元测试[即单元测试Authorize属性是否正常运行]?

3 个解决方案

#1


3  

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.

这是关于单元测试自定义授权组件的博客文章。它不涉及RoleProviders和MembershipProviders,但您应该能够有效地使用相同的方法来测试任意数量的过滤器。

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.

免责声明:可能需要几个解析才能理解它 - 它写得不是很清楚,所提供的示例代码也没有完全切入追逐,但你应该能够从中收集你需要的东西。

#2


1  

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.

您可以使用Ivonna,一种商业工具,或免费的MvcIntegrationTestFramework。两者都支持在进程中测试你的视图,但我会让你模拟其余的,调整你的配置等,所以它更多的是在单元测试方面。

#3


0  

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

对于UI(控制器和向下)下的集成测试,您可以使用Fit,FitNesse,Slim甚至StoryTeller之类的东西。

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

对于UI测试(您的视图),您应该考虑使用Selenium和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.

我意识到这是一个很多信息,但是当它涉及更强大的测试解决方案时它会很适合你。

#1


3  

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.

这是关于单元测试自定义授权组件的博客文章。它不涉及RoleProviders和MembershipProviders,但您应该能够有效地使用相同的方法来测试任意数量的过滤器。

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.

免责声明:可能需要几个解析才能理解它 - 它写得不是很清楚,所提供的示例代码也没有完全切入追逐,但你应该能够从中收集你需要的东西。

#2


1  

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.

您可以使用Ivonna,一种商业工具,或免费的MvcIntegrationTestFramework。两者都支持在进程中测试你的视图,但我会让你模拟其余的,调整你的配置等,所以它更多的是在单元测试方面。

#3


0  

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

对于UI(控制器和向下)下的集成测试,您可以使用Fit,FitNesse,Slim甚至StoryTeller之类的东西。

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

对于UI测试(您的视图),您应该考虑使用Selenium和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.

我意识到这是一个很多信息,但是当它涉及更强大的测试解决方案时它会很适合你。