I have a solution with differents projects in it, One those is an MVC 3 project, and the other is a test project.
From the test project, I'm trying to test my controllers behavior. But when I'm running a test the system throws an error telling me this:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified.
WRN: Assembly binding logging is turned OFF.To enable assembly bind failure logging, set the registry value[HKLM\Software\Microsoft\Fusion!EnableLog]
(DWORD) to 1.
The code from the test os the following:
[TestMethod]
public void TestMethod()
{
var myService = new Mock<IMyService>();
myService.Setup(a => a.ReadAuthorizationRequest())
.Returns<EndUserAuthorizationRequest>(null);
int error = 0;
var controller = new myController(myService.Object);
try
{
var actionResult = controller.Authorize();
}
catch (HttpException exception)
{
error = exception.GetHttpCode();
}
Assert.AreEqual(403, error);
}
the controller:
public class myController: Controller
{
private readonly IMyService _service;
public OAuthController(IMyService service)
{
_service = service;
}
[Authorize, AcceptVerbs(HttpVerbs.Get | HttpVerbs.Post)]
public ActionResult Authorize()
{
ClientApplication requestingClient;
var request = _service.ReadAuthorizationRequest();
if (request == null)
{
throw new HttpException((int) HttpStatusCode.BadRequest,
"Missing authorization request.");
}
}
}
As you can see, I'm trying to test the controller behavior, so that, whenever the service doesn't read the request the controller should throw an httpException with httpStatusCode 403.
What is wrong with this. Thank you in advance.
From your exception:
System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.Mvc, Version=1.0.0.0
It is trying to load MVC version 1. You probably need to do an assembly binding redirect in your test project as well, same as the one in the web.config:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
<bindingRedirect oldVersion="1.0.0.0-2.0.0.0" newVersion="3.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
Place that in the configuration
section of the app.config of your test assembly.
I had the same error after I installed VS 2012, none of the projects in my solution references the System.Web.Mvc. The issue was resolved for me when I added the System.Web.Mvc.dll to the GAC.
Here are the steps I did:
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With