1

I have a HtmlHelper that returns the bootstrap selected class. I use this helper to apply the active state to my menu items.

Helper Code

public static string IsSelected(this HtmlHelper html, string controllers = "", string actions = "",
      string ccsClass = "selected")
{
  var viewContext = html.ViewContext;
  var isChildAction = viewContext.Controller.ControllerContext.IsChildAction;

  if (isChildAction)
  {
    viewContext = html.ViewContext.ParentActionViewContext;
  }

  var routeValues = viewContext.RouteData.Values;
  var currentController = routeValues["controller"].ToString();
  var currentAction = routeValues["action"].ToString();

  if (string.IsNullOrEmpty(controllers))
  {
    controllers = currentController;
  }

  if (string.IsNullOrEmpty(actions))
  {
    actions = currentAction;
  }

  var acceptedActions = actions.Trim().Split(',').Distinct().ToArray();
  var acceptedControllers = controllers.Trim().Split(',').Distinct().ToArray();

  return acceptedControllers.Contains(currentController) && acceptedActions.Contains(currentAction)
    ? ccsClass
    : string.Empty;
}

Testing Code

[Test]
public void WhenPassedControllerAndActionMatchContextReturnSelectedClass()
{
  var htmlHelper = CreateHtmlHelper(new ViewDataDictionary());
  var result = htmlHelper.IsSelected("performance", "search");

  Assert.AreEqual("selected", result);
}

public static HtmlHelper CreateHtmlHelper(ViewDataDictionary viewData)
{
  var mocks = new MockRepository();

  var controllerContext = mocks.DynamicMock<ControllerContext>(
    mocks.DynamicMock<HttpContextBase>(),
    new RouteData(),
    mocks.DynamicMock<ControllerBase>());

  var viewContext = new ViewContext(controllerContext, mocks.StrictMock<IView>(), viewData, new TempDataDictionary(), mocks.StrictMock<TextWriter>());

  //var mockViewContext = MockRepository.GenerateMock<ViewContext>();

  var mockViewDataContainer = mocks.DynamicMock<IViewDataContainer>();

  mockViewDataContainer.Expect(v => v.ViewData).Return(viewData);

  return new HtmlHelper(viewContext, mockViewDataContainer);
}

This is giving me an error. When I debug, I see that it is because ControllerContext is null on Line 5 of Helper code.

What would be the flexible, correct way of testing that code?

1 Answer 1

1

I've noticed that your are using rhino-mocks but it possible (and really easy) to solve your problem using Typemock Isolater by faking the dependencies of HtmlHelper as shown in the following example:

[TestMethod, Isolated]
public void WhenPassedControllerAndActionMatchContextReturnSelectedClass()
{
    var fakeHtmlHalper = Isolate.Fake.Dependencies<HtmlHelper>();
    var fakeViewContext = Isolate.GetFake<ViewContext>(fakeHtmlHalper);

    Isolate.WhenCalled(() => fakeViewContext.RouteData.Values["controller"]).WillReturn("performance");
    Isolate.WhenCalled(() => fakeViewContext.RouteData.Values["action"]).WillReturn("search");

    var result = fakeHtmlHalper.IsSelected("performance", "search");

    Assert.AreEqual("selected", result);

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.