Recently I’ve been working a lot with the new ASP.NET MVC framework, with Preview 1 (bleh) and the interim drop, Pre-Preview 3?
The ActionResult is a fantastic addition, making the framework far easier to test. However, I noticed I was basically doing the same thing for each test:
-
[Test]
-
public void TestSomething()
-
{
-
var actionResult = controllerUT.Something() as RenderViewResult;
-
-
If ( actionResult == null )
-
Assert.Fail(“ActionResult was null.”);
-
-
Assert.
That(actionResult.
ViewName,
Is.
EqualTo(“ViewName”
));
-
-
Assert.
That(((SomeClass
)actionResult.
ViewData).
SomeProperty,
Is.
EqualTo(123));
-
}
Basically asserting the view name, view data type and maybe asserting a property or two on the view data. In the spirit of treating the test code as a first class citizen I decided to refactor out the duplication. The resulting refactoring turned the above into the following:
-
[Test]
-
public void TestSomething()
-
{
-
controller.
Something().
AssertViewName(“ViewName”
).
AssertViewDataType<SomeClass>
(c => Assert.
That(c.
SomeProperty,
Is.
EqualTo(123));
-
}
Which I personally think is alot nicer! Although I'm a little biased, so I thought I would share it and see what other people thought of it. Below is the code I came up with. Any and all suggestions (good or bad) are appreciated
-
namespace e4umsMN.Spec.Utils
-
{
-
using System;
-
using System.Web.Mvc;
-
using NUnit.Framework;
-
using NUnit.Framework.SyntaxHelpers;
-
-
internal static class ActionResultHelper
-
{
-
-
#region Public statics (6)
-
-
public static ActionResult AssertActionRedirect(this ActionResult actionResult, string actionName)
-
{
-
return actionResult.
AssertNotNull<ActionRedirectResult>
(arr => Assert.
That(arr.
Values["action"],
Is.
EqualTo(actionName
)));
-
}
-
-
public static ActionResult AssertHttpRedirect(this ActionResult actionResult, string redirect)
-
{
-
return actionResult.
AssertNotNull<HttpRedirectResult>
(hrr => Assert.
That(hrr.
Url,
Is.
EqualTo(redirect
)));
-
}
-
-
public static ActionResult AssertRenderViewResult(this ActionResult actonResult, Action<RenderViewResult> extraAssert)
-
{
-
return actonResult.AssertNotNull(extraAssert);
-
}
-
-
public static ActionResult AssertViewDataType<T>(this ActionResult actionResult, Action<T> viewDataAssert)
-
{
-
return actionResult.AssertRenderViewResult(a => viewDataAssert((T)a.ViewData));
-
}
-
-
public static ActionResult AssertViewDataType<T>(this ActionResult actionResult)
-
{
-
return actionResult.
AssertRenderViewResult(a => Assert.
That(a.
ViewData,
Is.
TypeOf(typeof(T
))));
-
}
-
-
public static ActionResult AssertViewName(this ActionResult actionResult, string viewName)
-
{
-
return actionResult.
AssertRenderViewResult(a => Assert.
That(a.
ViewName,
Is.
EqualTo(viewName
)));
-
}
-
-
#endregion Public statics
-
-
#region Other statics (1)
-
-
private static T AssertNotNull<T>(this object actionResult, Action<T> extraAssert)
-
{
-
if (actionResult == null)
-
Assert.Fail("ActionResult was null");
-
-
extraAssert((T) actionResult);
-
-
return (T) actionResult;
-
}
-
-
#endregion Other statics
-
-
}
-
}