Skip to content

Easy ways to test Asp & Aps.Mvc pages and routes

License

Notifications You must be signed in to change notification settings

approvals/ApprovalTests.Net.Asp

Repository files navigation

Approvals.Net.Asp

Build status License NuGet Status

Allows you to write simple tests against Asp & Mvc pages.

Mvc Pages

Testing Rendered Mvc pages is fairly simple The main code is

 MvcApprovals.VerifyMvcPage<YourTestableController>(c => c.TestName);

Full Example

The Main points are:

  1. Using CassiniDevServer to host a webserver at test time
  2. Creating a TestableController Page to call
  3. Adding UnitTestBootStrap.RegisterWithDebugCondition("YourAssembyName"); to your Global.asax
  4. Using .Explicit() on your views or extending : ControllerWithExplicitViews

Routes

You can also easily test your routes

[TestMethod]
public void TestRoutes()
{
	var urls = new[] {"/Home/Index/Hello", "/"};
	AspApprovals.VerifyRouting(MvcApplication.RegisterRoutes, urls);
}

Which will product Golden Master Files like

/Home/Index/Hello => [[controller, Home], [action, Index], [id, Hello]] 
/ => [[controller, Cool], [action, Index], [id, ]] 

Asp Pages

Approvals can also unit test any urls that produce reliable output (think static pages).
Here's a good video to explain all the inner workings

Available on NuGet

Install-Package ApprovalTests.Asp

LICENSE

Apache 2.0 License

Contributors

The vast majority of this was created by @jamesrcounts & @Lnknaveen with help from @isidore

Explicit Names

If you need to add seams into your production code for testing, you will need the following added to you runtime

using System;
using System.Web.Mvc;
using ApprovalUtilities.CallStack;

public static class MvcUtilites
{
	public static ViewResult CallViewResult<T>(Func<T, ActionResult> call, T parameter)
	{
	    var actionResult = (ViewResult) call(parameter);
	    actionResult.ViewName = call.Method.Name;
	    return actionResult;
	}

	public static ViewResult Explicit(this ViewResult view)
	{
	    view.ViewName = new Caller().Method.Name;
	    return view;
	}
}