Mock presence of Authorize attribute

你说的曾经没有我的故事 提交于 2019-12-02 12:13:18

问题


I was wondering how you could mock that a controller is decorated with the [Authorize] attribute in MVC3? I am using a custom membership provider. I would like to test that a controller been decorated with the attribute and you are authorized and what happens when you are not. I'm using Moq. Any good suggestions on this?!

UPDATE: I'm currently getting an NullreferenceException saying "object reference not set to an instance of an object". It's the same error as previously mentioned in this post NullReferenceException while using Authorize Attribute

This is related to the Authorize attribute. I'm running under iis and not using cassini. Does anyone know if this is somehow related to applicaton pool and user rights. The tests that I have for the authorize attribute wont work until this is fixed.

Maybe there is some other way of testing the Authorize attribute? Big thanks in advance.

UPDATE2 So after some extensive research and debugging help from a colleague I mentioned to fix the problem with the [Authorize] attribute. It appears as though this line in the web.config was missing:

<modules runAllManagedModulesForAllRequests="true">

Strangely enough this did not cause problem for another colleague that was sharing the trunk or in beta environment. It only caused problems for me locally. This might have been related to something in the GAC. Anyhow all works now.

Thanks.


回答1:


I wrote a blog post about exactly that a couple of months ago:

http://thomasardal.com/unit-testing-attribute-decorations/



来源:https://stackoverflow.com/questions/7167251/mock-presence-of-authorize-attribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!