Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC3 upgrade - "Attempt by method [whatever] to access method System.Web.Mvc.Controller.View(...) failed"

I have a unit test that worked fine in MVC2. The test simply defines the Action on the controller, the necessary stubs, and tests the name of the view. However, after upgrading to MVC3, when I invoke the method, I get the error above. The site MVC3 upgrade works just fine; I just have these unit tests failing due to the upgrade. Thanks.

Here's my action:

public partial class GadgetController
{
    [SetterProperty]
    public IATCGadgetProxy ATCGadgetService { get; set; }

    public ActionResult LoadForums(bool popularOnly, bool myThreads, int itemCount)
    {
        var model = ATCGadgetService.LoadForums(popularOnly, myThreads, itemCount);

        return View("AskTheCommunity-Forums", model);
    }
}

Here's the test. It is failing when it's returning the view from the Action.

[TestMethod]
public void Test_Forums_Action_Type()
{
    GadgetController controller = new GadgetController();
    controller.ATCGadgetService = new ATCGadgetServiceStub();
    ViewResult result = controller.LoadForums(false, false, 10) as ViewResult;

    Assert.IsNotNull(result);
    Assert.AreEqual("AskTheCommunity-Forums", result.ViewName);
}
like image 850
Ryan Peters Avatar asked Jul 14 '11 19:07

Ryan Peters


2 Answers

I know this is an old thread but I just got the same error with MVC 5.2.3... but using Xunit. In the end, it does not really matter as the way to solve the issue would be the same.

First, you have to make sure MVC was added to your Tests project. I added it via NuGet:

Install-Package Microsoft.AspNet.Mvc -Version 5.2.3

Or you can alter the version to whatever MVC version you are using.

Then, I was still having the error. I just found out that the App.config page was missing information. Once I made sure I had these lines, everything worked:

<?xml version="1.0" encoding="utf-8"?>
<configuration>

    <!-- Other config here -->

    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" culture="neutral" />
                <bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>
like image 142
Maxime Avatar answered Nov 11 '22 15:11

Maxime


One of the things that drove me crazy upgrading a project to MVC 3 was these odd, unexplained errors. Until I figured that not all of the projects were upgraded to MVC 3 (in your case - that might be the test project) and remained in MVC 2, which caused some very weird behaviors like the one you describe. Please check the version of System.Web.Mvc (and possibly related assemblies) in your test project.

like image 3
Ofer Zelig Avatar answered Nov 11 '22 15:11

Ofer Zelig