views:

205

answers:

2

I am using Moq for unit testing and I would like to test for a view's attribute. In this case the Authorize attribute.

Example View Code:

[Authorize(Roles = "UserAdmin")]
public virtual ActionResult AddUser()
{
   // view logic here  
   return View();
}

So I would like to test the view attribute when I act on this view with a user that is in the role of UserAdmin and a user that is not in the role of user admin. Is there anyway to do this ?

Example Test:

[Test]
public void Index_IsInRole_Customer()
{
   // Arrange
   UserAdminController controller = _controller;
   rolesService.Setup(r => r.IsUserInRole(It.IsAny<string>(), It.IsAny<string>())).Returns(false); // return false for any role

   // Act
   var result = controller.AddUser();

   // Assert
   Assert.IsNotNull(result, "Result is null");
}
A: 

When executing the test above the AuthorizeAttribute will not be taken into account (that is, no one will evaluate it). This is normally the responsibility of the ControllerActionInvoker (a class in System.Web.Mvc).

You might want to just trust that AuthorizeAttribute is correctly implemented. Then just use reflection to verify that the AuthorizeAttribute has been correctly defined on your action.

Rune
+5  A: 

Attributes are just metadata on the type, so they don't do anything unless the surrounding infrastructure make them do something (or better yet: the surrounding infrastructure does something based on the information in those attributes). That's what the ASP.NET MVC framework does when it executes a request.

That is not what you do when you create and invoke a Controller Action in a unit test, so unless you want to go to great lengths to invoke the Controller Action using a ControllerActionInvoker (at which point the test ceases to be a unit test and becomes an integration test) you can't directly test the behavior implied by the attribute.

You can, however, write a unit test that verifies that the attribute correctly decorates the Controller Action:

var attributes = typeof(UserAdminController)
    .GetMethod("AddUser").GetCustomAttributes(true);
var result = attributes.OfType<AuthorizeAttribute>().Single();
Assert.AreEqual("UserAdmin", result.Roles);
Mark Seemann
Yea maybe my question should have been, *should* I test the action attributes. but nice answer thanks dude! +1
gmcalab
I have one more question... What if I have two action by the same name one if a GET and one is a Post....When I use GetMethod I get an error for ambiguous results found. How to I differentiate?
gmcalab
@gmcalab: Then you will need to use the `GetMethod(string, Type[])` overload.
Mark Seemann
Thanks a lot! +1
gmcalab