无法加载文件或程序集'System.Web.Mvc

Dan*_*iel 6 asp.net c#-4.0 asp.net-mvc-3

我有一个不同项目的解决方案,一个是MVC 3项目,另一个是测试项目.

从测试项目中,我正在尝试测试我的控制器行为.但是当我运行测试时,系统会抛出一个错误告诉我:

System.IO.FileNotFoundException:无法加载文件或程序集'System.Web.Mvc,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一.该系统找不到指定的文件.
警告:程序集绑定日志记录已关闭.要启用程序集绑定失败日志记录,请将注册表值[HKLM\Software\Microsoft\Fusion!EnableLog](DWORD)设置为1.

测试中的代码如下:

[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);
}
Run Code Online (Sandbox Code Playgroud)

控制器:

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.");
        }
     }
}
Run Code Online (Sandbox Code Playgroud)

正如您所看到的,我正在尝试测试控制器行为,因此,只要服务不读取请求,控制器就应该使用httpStatusCode 403抛出httpException.

这有什么问题.先感谢您.

vcs*_*nes 12

从你的例外:

System.IO.FileNotFoundException:无法加载文件或程序集'System.Web.Mvc,Version = 1.0.0.0

它正在尝试加载MVC版本1.您可能还需要在测试项目中执行程序集绑定重定向,与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>
Run Code Online (Sandbox Code Playgroud)

将其放在configuration测试程序集的app.config部分中.