如何在 nUnit 中对我的 asp.net mvc 应用程序的视图和授权属性进行单元测试?
我现在想开始为我的 MVC 应用程序编写单元测试。我已经弄清楚如何对控制器进行单元测试,并且可以毫无问题地对我的底层业务库进行单元测试。不过,我在几个项目上遇到了困难:
如何对我的视图进行单元测试?也就是说,在控制器返回正确的视图之后(我已经对其进行了单元测试,并进行了单元测试以检查它是否传递了正确的数据)。有没有一种方法可以对视图在 nUnit 中的正确字段中显示正确的数据进行单元测试,或者我是否需要其他工具来实现此目的?
如何对安全性进行单元测试?我正在使用自定义成员资格提供程序和角色提供程序,我认为它们本身可以像任何其他组件一样进行单元测试 - 我只是更新提供程序并检查每个方法是否返回预期输出。我很好奇的是,如何对我的每个控制器方法进行单元测试,以确保我的每个控制器方法都从我的提供商处接收到正确的安全信息[即对 Authorize 属性是否正常运行进行单元测试]?
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]?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是关于单元测试自定义授权组件的博客文章。它不涉及 RoleProviders 和 MembershipProviders,但您应该能够有效地使用相同的方法来测试任意数量的过滤器。
http://darioquintana .com.ar/blogging/2009/05/23/aspnet-mvc-testing-a-custom-authorize-filters/
免责声明:可能需要几次解析才能理解它 - 它写得不是很清楚并且提供的示例代码也并不完全切入主题,但您应该能够从中收集到您需要的内容。
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.
您可以使用商业工具Ivonna,或MvcIntegrationTestFramework,免费的。两者都支持在进程中测试您的视图,但 Ivonna 允许您模拟其余部分,调整您的配置等,因此更多的是在单元测试方面。
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.
对于 UI(控制器及以下)下方的集成测试,您可以使用类似 Fit、FitNesse、苗条,甚至讲故事的人。
对于 UI 测试(您的视图),您应该考虑使用 Selenium 和 Selenium RC.
我意识到这是很多信息,但当涉及到更强大的测试解决方案时,它会非常适合您。
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.