Glimpse

Few years back, Phil Haack introduced a Routing Debugger. You might have also seen this niceMvc Route Debugger Visualizer.

What I’m about to show you is a really cool tool, which allows you to debug ASP.NET MVC applications. It is called Glimpse. Glimpse includes not only route debugging support but also many other features. As you know Firebug is a debugging tool for client, and Glimpse does it for the server. Glimpse only works with the version MVC3. It can support MVC2, however you need to have MVC3 installed. It should support ASP.NET Web Forms as well but the current release (beta version) has dependencies to MVC.  I haven’t tried the Glimpse Web Forms debugging yet, but it is supported.

NuGet:

PM> Install-Package Glimpse

glimpse

 

 

 

 

 

 

 

You can configure Glimpse by navigating to

http://localhost:/Glimpse/config

Below is how you see Glimpse while debugging information.

glimpse2

You can debug : Application settings, routes, session variables, trace data.

If it is an Ajax request, then the server embeds the JSON in the content of the page. And many more…

You can see a nice video and more information here.

So if you haven’t started using this, I suggest you download it, as it will definitely assist your development work!

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);
	}
}