开发者

Moq and Url.Content

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 cur开发者_StackOverflow社区rent 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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜