0

I'm unit testing someone else's ASP.Net MVC4 Controller action method. Its last line is:

return this.View("ConfirmAddress", addressModel);

This returns null in my unit test. The Controller.View documentation says the first parameter is a view name but I can't step into this method to find out why null is returned. A ConfirmAddress.cshtml exists in the Views folder but there's also a ConfirmAddress(AddressModel am) action in the controller.

Can anyone tell from this what it should be doing (e.g. perhaps use RedirectToAction instead???) Have tried to keep this short but could provide more info if needed...

Steve Chambers
  • 37,270
  • 24
  • 156
  • 208
  • What does your View folder tree look like (and where is the file in it), and from what Controller are you calling this? – Twon-ha Apr 11 '13 at 11:29
  • I'm calling this from `ApplicantController` and part of the view folder tree is `Views\Applicant\ConfirmAddress.cshtml` – Steve Chambers Apr 11 '13 at 11:32
  • 1
    Actually you can debug ASP.NET MVC source code using source code stepping: http://weblogs.asp.net/gunnarpeipman/archive/2010/07/04/stepping-into-asp-net-mvc-source-code-with-visual-studio-debugger.aspx – Erik Schierboom Apr 11 '13 at 11:34
  • @ErikSchierboom Hmmm actually seems like a world of pain trying to get this to work. The linked article didn't work for me (VS2012 Update 1) and I've followed various instructions in other places, all with no success... then came across this http://stackoverflow.com/questions/8139269/how-to-enable-enable-net-framework-source-stepping – Steve Chambers Apr 11 '13 at 13:03
  • And another offputting answer here: http://social.msdn.microsoft.com/Forums/en-US/refsourceserver/thread/41388c7b-582b-4e3f-8178-3d38a3c99639#a46efedc-8315-468b-9839-d66d5a06b3d3 Actually my advice to anyone stumbling across this is steer well clear of this feature! – Steve Chambers Apr 11 '13 at 13:10
  • @SteveChambers I didn't know it was so hard! A while ago it worked fine for me, but seems like things have taken a turn for the worse. Sorry that it wasn't helping you. – Erik Schierboom Apr 11 '13 at 13:28
  • @ErikSchierboom Well in the absence of any better answers yet I'm still investigating... It's one of those where it *may* be possible to get it working with a lot of thrashing around but certainly the official way doesn't work - thanks anyway! – Steve Chambers Apr 11 '13 at 13:33

2 Answers2

2

I have looked at the official source code of the Controller class to see what happens when View is called. It turns out, all the different View method overloads ultimately call the following method:

protected internal virtual ViewResult View(string viewName, string masterName, object model)
{
    if (model != null)
    {
        ViewData.Model = model;
    }

    return new ViewResult
    {
        ViewName = viewName,
        MasterName = masterName,
        ViewData = ViewData,
        TempData = TempData,
        ViewEngineCollection = ViewEngineCollection
     };
}

This method (and thus all the other overloads) will never return NULL, although it could throw an exception. It is virtual though, which means that the code your are calling might override it with a custom implementation and return NULL. Could you check if the View method is overridden anywhere?

Erik Schierboom
  • 16,301
  • 10
  • 64
  • 81
  • Couldn't find it anywhere I'm afraid. Have looked through all View methods in the solution and none seem to be overloading it. – Steve Chambers Apr 11 '13 at 14:44
1

It's possible that the View method is overridden. Try removing the this quantifier.

return View("ConfirmAddress", addressModel);
Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • Thanks but removing `this.` made no difference - still returns null. Have tried searching the whole solution for `ViewResult View` in case it is overloaded but no results were returned. – Steve Chambers Apr 11 '13 at 14:36
  • Without being able to look at more of your code I'd say your best bet is to try enabling symbols server support in VS and attempt to debug into the MVC framework. – Nick Albrecht Apr 11 '13 at 14:39
  • Thanks - would love to do that but unfortunately System.Web.Mvc.dll is the one and only module that has a "Symbol Status" of "Cannot find or open the PDB file." The DLL version number is 4.0.20710.0 and I've been unable to find a corresponding System.Web.Mvc.pdb for this anywhere... :-( – Steve Chambers Apr 11 '13 at 14:42