Question

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.

Was it helpful?

Solution

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

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top