0

First, let me say I've searched on this topic and did come across a solution here:

asp.net mvc: how to mock Url.Content("~")?

However, this solution involves creating a new base controller which, in my current scenario, is not something I have permission to do. So, the question...

I'm writing some unit tests currently for an MVC3 app (the first written by my current employer so I really am trying to drive home the benefits of unit testing [while being relatively new to unit testing myself]) and I've hit a snag with Url.Content

The application has an action as follows:

    [HttpGet]
    public ActionResult SearchResults()
    {
    ...
    ...
    ...

    if(results == null)
    {
        DisplayMessageInView("...Some display message...");
        return RedirectToAction("Index");
    }

where DisplayMessageInView is defined as

    private void DisplayMessageInView(string message)
    {            
        string imageUrl = Url.Content("~/Content/Images/109_AllAnnotations_Info_48x48_72.png");

        TempData["AlertMessage"] = string.Format("<img alt='info' src='{0}' />&nbsp;&nbsp;{1}", imageUrl, message);
    }

So, they're building a message and an image to diplay to the user on the Index action. Url.Content gets in the way of unit testing this so I'm looking for suggestions to resolve/work around this.

I do have an alternative solution that involves a little more effort and refactoring, but figured this was a good discussion point either way as I've run up this a few times prior and always had to do something a little out of the ordinary.

Short of the solution above, what other options are there to push beyond this? Your input is appreciated.

Thank You

Community
  • 1
  • 1
Khepri
  • 9,547
  • 5
  • 45
  • 61
  • Why don't you have the permission to improve your companies code base for the better? – slandau May 03 '11 at 17:33
  • Slandau, if it comes to that, I'll press the issue, but this is a solution that from the business perspective is *done*. Therefore, getting changes implemented at this point will be much more challenging for something that isn't viewed as value add. This is a culture that I'm trying to change at this time so if I can avoid changes to the solution itself while still being able to implement unit testing it would be a win. – Khepri May 03 '11 at 17:45
  • I find the notion of testing url.content to be an exercise in uselessness. Adding unit tests for that isn't going to help convince your organization that testability is anything more than a time sink. – Chris Marisic May 03 '11 at 19:11

1 Answers1

0

The design of the controller is incorrect and no amount of unit testing is going to fix that. Get them to allow you to correct the code and simply return an appropriate View on a null result, possibly setting a message in the ViewBag if required. Inside the view you will then display the appropriate image and message.

Darren Lewis
  • 8,338
  • 3
  • 35
  • 55
  • Kind of what I figured. So one last question. Would you re-use the existing view and just place some conditional logic in the view as to what to actually display or would you create an entirely new NoSearchResults view? – Khepri May 03 '11 at 18:00
  • The latter if it's likely to be used elsewhere. Depending on what's in the result you could also implement the null object pattern and return the same view. My preference without seeing more code though is the NoSearchResults view. – Darren Lewis May 03 '11 at 18:02