ASP.NET MVC3 Update - "An attempt to [independently] access the System.Web.Mvc.Controller.View (...) method failed"

I have a unit test that worked fine in MVC2. The test simply determines the action on the controller, the necessary stubs, and checks the name of the view. However, after upgrading to MVC3, when I call the method, I get the error above. Updating the MVC3 site works fine; Due to the update, I just refuse these unit tests. Thanks.

Here is 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 is the test. It does not work when it returns a view from an 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); } 
+6
source share
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, since the way to solve the problem will be the same.

First, you need to make sure that MVC has been added to your Tests project. I added it through NuGet:

Microsoft.AspNet.Mvc Installation Package -Version 5.2.3

Or you can change the version to any version of MVC that you use.

Then I still had a mistake. I just found out that there was no information on the App.config page. As soon as I was convinced that 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> 
+4
source

One of the things that made me crazy about upgrading a project to MVC 3 was these strange inexplicable errors. Until I realized that not all projects were upgraded to MVC 3 (in your case, it could be a test project) and remained in MVC 2, which caused some very strange behaviors like the ones you describe. Please check the version of System.Web.Mvc (and possibly related assemblies) in the test project.

+3
source

Source: https://habr.com/ru/post/892786/


All Articles