views:

85

answers:

1

Hi, I have an action filter which i got from the below link http://blog.wekeroad.com/blog/aspnet-mvc-securing-your-controller-actions/

there is something called "RequiresAuthenticationAttribute" for this i need to write test case. how can i do this? form some of the blogs i read that we need to mock httcontext. How can i mock this? what is the procedure that i need to do? is there any link for this?

+2  A: 

Don't use the [RequiresAuthentication] attribute from Rob's blog. It's meant for a very old pre-release version of MVC. Use the in-box [Authorize] attribute instead.

Since the [Authorize] attribute is written by the MVC team, you don't need to unit test its logic. However, if you want, you can verify that it's applied to your controllers or actions. Simply get the Type or MethodInfo you're interested in, then call its GetCustomAttributes() method to get instances of AuthorizeAttribute. You can inspect those instances for the values that you expect.

If you want, you can look at the source code of AuthorizeAttribute for information on writing your own filter. Additionally, you can look at the official unit test of this type, so if you do end up writing a filter you can use a similar method to write your own type's unit tests.

Levi
Thanks Levi,in the unit test link, i could see they are using "Mock<HttpContextBase> mockHttpContext = new Mock<HttpContextBase>();"from where can i get that dll? is this the right way to do?like RequiresAuthentication attribute, i do have my own custom filters.. to acquire the code coverage i need to write test case for filters as well.
Nimesh
The MVC team uses the MOQ mocking framework. You can get more information at http://code.google.com/p/moq/. But you're free to use whatever you're comfortable with.
Levi