Geeks With Blogs

@chriscanal
  • chriscanal "Insufficient stack to continue executing the program safely." Well... that's new about 316 days ago
  • chriscanal @nonimage It's not possible to do anything other than unspeakable things with PHP about 316 days ago
  • chriscanal Email from recruiter "The software is being built using .NET technology (.NET 3.5, MVC, WCF, Unity, Dependency) " uh, yea... about 327 days ago
  • chriscanal . @chrissie1 Dodgy maths using the dynamic keyword, result of dividing by zero about 327 days ago
  • chriscanal Expected: 100 But was: Infinity Well... that's not gone to plan... about 327 days ago

Chris Canal A Scottish .NET Developer
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:
  1. [Test]
  2. public void TestSomething()
  3. {
  4.    var actionResult = controllerUT.Something() as RenderViewResult;
  5.  
  6.    If ( actionResult == null )
  7.       Assert.Fail(“ActionResult was null.”);
  8.  
  9.    Assert.That(actionResult.ViewName, Is.EqualTo(“ViewName”));
  10.    Assert.That(actionResult.ViewData, Is.Typeof(typeof(SomeClass)));
  11.    Assert.That(((SomeClass)actionResult.ViewData).SomeProperty, Is.EqualTo(123));
  12. }
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:
  1. [Test]
  2. public void TestSomething()
  3. {
  4.    controller.Something().AssertViewName(“ViewName”).AssertViewDataType<SomeClass>(c => Assert.That(c.SomeProperty, Is.EqualTo(123));
  5. }
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
  1. namespace e4umsMN.Spec.Utils
  2. {
  3.     using System;
  4.     using System.Web.Mvc;
  5.     using NUnit.Framework;
  6.     using NUnit.Framework.SyntaxHelpers;
  7.  
  8.     internal static class ActionResultHelper
  9.     {
  10.  
  11.         #region Public statics (6)
  12.  
  13.         public static ActionResult AssertActionRedirect(this ActionResult actionResult, string actionName)
  14.         {
  15.             return actionResult.AssertNotNull<ActionRedirectResult>(arr => Assert.That(arr.Values["action"], Is.EqualTo(actionName)));
  16.         }
  17.  
  18.         public static ActionResult AssertHttpRedirect(this ActionResult actionResult, string redirect)
  19.         {
  20.             return actionResult.AssertNotNull<HttpRedirectResult>(hrr => Assert.That(hrr.Url, Is.EqualTo(redirect)));
  21.         }
  22.  
  23.         public static ActionResult AssertRenderViewResult(this ActionResult actonResult, Action<RenderViewResult> extraAssert)
  24.         {
  25.             return actonResult.AssertNotNull(extraAssert);
  26.         }
  27.  
  28.         public static ActionResult AssertViewDataType<T>(this ActionResult actionResult, Action<T> viewDataAssert)
  29.         {
  30.             return actionResult.AssertRenderViewResult(a => viewDataAssert((T)a.ViewData));
  31.         }
  32.  
  33.         public static ActionResult AssertViewDataType<T>(this ActionResult actionResult)
  34.         {
  35.             return actionResult.AssertRenderViewResult(a => Assert.That(a.ViewData, Is.TypeOf(typeof(T))));
  36.         }
  37.  
  38.         public static ActionResult AssertViewName(this ActionResult actionResult, string viewName)
  39.         {
  40.             return actionResult.AssertRenderViewResult(a => Assert.That(a.ViewName, Is.EqualTo(viewName)));
  41.         }
  42.  
  43.         #endregion Public statics
  44.  
  45.         #region Other statics (1)
  46.  
  47.         private static T AssertNotNull<T>(this object actionResult, Action<T> extraAssert)
  48.         {
  49.             if (actionResult == null)
  50.                 Assert.Fail("ActionResult was null");
  51.  
  52.             extraAssert((T) actionResult);
  53.  
  54.             return (T) actionResult;
  55.         }
  56.  
  57.         #endregion Other statics
  58.  
  59.     }
  60. }
Posted on Thursday, May 15, 2008 6:59 PM development , tdd , aspnetmvc | Back to top

Copyright © Chris Canal | Powered by: GeeksWithBlogs.net | Join free