Unable to convert from HttpContextBase to HttpContextBase

With the following code (using Moq 4.0.10501.6):

HomeController controller = new HomeController();
ActionResult result = _controller.Index();

Mock<HttpResponseBase> response = new Mock<HttpResponseBase>();

Mock<HttpContextBase> httpContext = new Mock<HttpContextBase>();
httpContext.Setup(x => x.Response).Returns(response.Object);

Mock<ControllerContext> controllerContext = new Mock<ControllerContext>();
controllerContext.Setup(c => c.HttpContext).Returns(httpContext.Object);
result.ExecuteResult(controllerContext.Object);

... I get the following compiler errors:

error CS1502: The best overloaded method match for
'Moq.Language.IReturns<System.Web.Mvc.ControllerContext,
 System.Web.HttpContextBase>.Returns(System.Web.HttpContextBase)'
has some invalid arguments

error CS1503: Argument 1: cannot convert from 'System.Web.HttpContextBase
[c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\
v4.0\System.Web.dll]' to 'System.Web.HttpContextBase'

How am I wrong? Why can't I convert from HttpContextBaseto HttpContextBase?

I started with an ASP.NET MVC project, added a NUnit test project, and ReSharper parsed the missing System.Web link. According to the properties window in VS, the System.Web.dll I am referring to is in C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Web.dll.

+3
source share
4 answers

I found (thanks to Reflector) what HttpContextBaseis actually in System.Web.dll, Version 4.0.0.0and in System.Web.Abstractions, Version 3.5.0.0.

So, I fixed it by adding a link to System.Web.Abstractions, Version 4.0.0.0the project.

, System.Web.Abstractions, Version 4.0.0.0 HttpContextBase.

, , , , , .

+7

System.Web - System.Web.Abstractions HttpContextBase.

, HttpContextBase - System.Web.Abstractions.

+1

It seems that you were referring to different versions of the assembly System.Web.Mvc. Try to remove all references to this assembly in your projects and add it from the GAC.

0
source

I wonder if this can be fixed by redirecting the assembly instead of referencing the .NET 3.5 assembly in the .NET 4.0 project.

0
source

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


All Articles