【问题标题】:Test Authorize Controller测试授权控制器
【发布时间】:2012-09-14 14:05:46
【问题描述】:

我在 TDD Book for Test Authorize Controller 中看到了练习,但我不明白这是什么意思

Assert.IsTrue(typeof (TodoController)
                  .GetCustomAttributes(true).ToList()
                  .Any(o=>o.GetType()==typeof(AuthorizeAttribute))
);

【问题讨论】:

    标签: asp.net-mvc test-first


    【解决方案1】:

    他们正在检查[Authorize] 属性是否已添加到TodoController。请注意,他们实际上并没有测试授权机制是否有效,只是AuthorizeAttribute 装饰存在。

    【讨论】:

      【解决方案2】:

      它检查TodoController是否有AuthorizeAttribute,即用[Authorize]装饰

      [Authorize] // <-- if this is present the test will pass, if not it will fail.
      public class TodoController {
          // ...
      }
      

      【讨论】:

      • tnx.getCustomAttributes(true) 到底是什么
      • 它返回一个包含应用于该类型的所有自定义属性的数组。 agrgument 应设置为true 以搜索此成员的继承链以查找属性,否则为false。参考:msdn.microsoft.com/en-us/library/kff8s254.aspx
      猜你喜欢
      • 1970-01-01
      • 2011-07-02
      • 1970-01-01
      • 2014-02-10
      • 1970-01-01
      • 2013-08-29
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多