Simple test helper – MVC Action has decorated with a certain attribute

Below is a useful helper, which allows you to verify whether an Action is decorated with a certain attribute. This is very simple to implement, but you can use something like below.
Test helper:

public static class ActionAttributeTestHelper
{
	public static bool DoesMethodActionContainAttribute<TController, TActionResult, TAttribute>(Expression<Func<TController, TActionResult>> mExpression)
	{
		Expression<Func<TController, TActionResult>> methodExp = mExpression;
		var methodCall = (MethodCallExpression)methodExp.Body;
		var methodInfo = methodCall.Method;
		return methodInfo.GetCustomAttributes(false).Any(a => a is TAttribute);
	}
}



public class SutController : Controller
{
	[Authorize]
	public ActionResult ActionWithAttributeNoParameter()
	{
	   return View();
	}

	public ActionResult ActionWithNoAttribute()
	{
	   return View();
	}

	[Authorize]
	public ActionResult ActionWithAttributeWithParamter(int i)
	{
	   return View();
	}

	[Authorize]
	[HttpGet]
	[OutputCache]
	public ActionResult ActionWithMultipleAttributes()
	{
	   return View();
	}
}

Unit Tests:

public class SutControllerTest
{
	[TestMethod]
	public void ActionWithAttributeNoParameter_ReturnsTrue()
	{
		// Arrange
		var sut = new SutController();

		//Act
		var doesTheMethodContainAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, AuthorizeAttribute>((x) => sut.ActionWithAttributeNoParameter());

		//Assert
		Assert.IsTrue(doesTheMethodContainAttribute);
	}

	[TestMethod]
	public void ActionWithNoAttribute_ReturnsFalse()
	{
		// Arrange
		var sut = new SutController();

		//Act
		var doesTheMethodContainAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, AuthorizeAttribute>((x) => sut.ActionWithNoAttribute());

		//Assert
		Assert.IsFalse(doesTheMethodContainAttribute);
	}

	[TestMethod]
	public void ActionWithAttributeWithParamter_ReturnsTrue()
	{
		// Arrange
		int anyNumber = 7;
		var sut = new SutController();

		//Act
		var doesTheMethodContainAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, AuthorizeAttribute>((x) => sut.ActionWithAttributeWithParamter(anyNumber));

		//Assert
		Assert.IsTrue(doesTheMethodContainAttribute);
	}

	[TestMethod]
	public void ActionWithMultipleAttributes_ReturnsTrue()
	{
		// Arrange
		var sut = new SutController();

		//Act
		var doesTheMethodContainAuthAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, AuthorizeAttribute>((x) => sut.ActionWithMultipleAttributes());

		var doesTheMethodContainHttpGetAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, HttpGetAttribute>((x) => sut.ActionWithMultipleAttributes());

		var doesTheMethodContainOutputCacheAttribute = ActionAttributeTestHelper.DoesMethodActionContainAttribute<SutController, ActionResult, OutputCacheAttribute>((x) => sut.ActionWithMultipleAttributes());

		//Assert
		Assert.IsTrue(doesTheMethodContainAuthAttribute);
		Assert.IsTrue(doesTheMethodContainHttpGetAttribute);
		Assert.IsTrue(doesTheMethodContainOutputCacheAttribute);
	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s