简体   繁体   English

用Authorize属性修饰的单元测试操作

[英]unit test actions decorated with Authorize attribute

I have simple asp.net mvc application which uses custom membership and role provider . 我有一个简单的asp.net mvc应用程序,它使用自定义membershiprole provider After setting all in place I use [Authorize] inside controller actions to allow/deny access to certain user groups. 将所有位置设置到位后,我在控制器操作中使用[Authorize]来允许/拒绝对某些用户组的访问。 Now I want to move further and try to unit test every controller which uses [Authorize] attribute. 现在,我想更进一步,尝试对使用[Authorize]属性的每个控制器进行单元测试。

I was thinking to use Unity Ioc and Moq. 我当时正在考虑使用Unity Ioc和Moq。 Any good suggestion from where to start, basic steps with some code examples or tutorials to share? 从何处开始有什么好的建议,一些代码示例或教程的基本步骤可共享?

Thanks 谢谢

I would suggest the below approach. 我建议采用以下方法。 Basically you can write a Unit test to ensure your action is decorated with the [Authorize] attribute. 基本上,您可以编写单元测试,以确保您的操作使用[Authorize]属性进行修饰。

See my blog post for more information. 有关更多信息,请参见我的博客文章。 This approach should work nicely with your Unit Test. 这种方法应该可以很好地与单元测试一起使用。

http://blog.rajsoftware.com/post/2012/12/04/Simple-test-helper-MVC-Action-has-decorated-with-a-certain-attribute.aspx http://blog.rajsoftware.com/post/2012/12/04/Simple-test-helper-MVC-Action-has-decorated-with-a-certain-attribute.aspx

I answered this in another thread - the blog post link above is "ok", but I wanted to test the actual attribute on the actual controller I was testing: 我在另一个线程中回答了这个问题-上面的博客文章链接为“ ok”,但我想在我正在测试的实际控制器上测试实际属性:

Answer: How to test custom MVC attribute 答:如何测试自定义MVC属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Authorize属性为控制器操作编写失败的单元测试用例 - How to write failed unit test case for controller actions with Authorize attribute 对使用 [Authorize] 属性修饰的控制器进行集成测试 - Integration test on controller decorated with [Authorize] attribute c#WebApi如果使用具有特定参数的授权属性修饰控制器操作,如何进行单元测试 - c# WebApi how to unit test if a controller action is decorated with an authorization attribute with specific arguments 如何有效地测试动作是否用属性(AuthorizeAttribute)装饰? - How to efficiently test if action is decorated with an attribute (AuthorizeAttribute)? 从ContextBoundObject继承并用ContextAttribute装饰的单元测试类 - Unit test class inherited from ContextBoundObject and decorated with ContextAttribute 单元测试和属性 - Unit Test and attribute 具有DataSource属性的单元测试 - Unit test with DataSource attribute MVC MSpec测试未达到[Authorize]属性 - MVC MSpec test not hitting [Authorize] attribute 解释MVC授权属性如何执行类似AOP的操作 - Explain HOW the MVC Authorize Attribute performs AOP-like actions 为所有控制器的自定义策略和全局的操作设置authorize属性 - Set authorize attribute for Custom policy for all controller and Actions at global
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM