错误单元测试webapi控制器

reg*_*EAH 11 c# asp.net unit-testing json.net asp.net-web-api

我正在使用AspNet Web Api Client 5.0,我正在尝试对web api控制器进行单元测试.

var encservice = new EncryptionService();
var acctservice = FakeServices.GetAccountService();
var controller = new AccountController(acctservice, encservice);
controller.Request = new HttpRequestMessage();
Run Code Online (Sandbox Code Playgroud)

当代码

controller.Request.SetConfiguration(new HttpConfiguration());
Run Code Online (Sandbox Code Playgroud)

被执行我遇到了异常

消息:无法加载文件或程序集'Newtonsoft.Json,Version = 4.5.0.0,Culture = neutral,PublicKeyToken = 30ad4fe6b2a6aeed'或其中一个依赖项.定位的程序集的清单定义与程序集引用不匹配.(HRESULT异常:0x80131040)

资料来源: System.Net.Http.Formatting

Stacktrace:位于System.Net的System.Net.Http.Forttting.Medic中的System.Net.Http.Forttting.Medie中的System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()处的System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor() .HttpConfiguration.DefaultFormatters()位于System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection路由)的System.Web.Http.HttpConfiguration..ctor()位于EMR.Test.Controller.AccountControllerTest.Should_Get()的c中:\ PremiumProjectsCollection\emr\src\EMRAzure\EMRAzure\EMR.Test\Controller\AccountControllerTest.cs:第34行

我使用的newsoft.json的版本是6.0

我的配置文件中也有一个程序集重定向

 <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>
Run Code Online (Sandbox Code Playgroud)

我使用的测试运行器是MStest,VS2012

Mar*_*ann 5

您需要添加程序集重定向:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json"
                          publicKeyToken="30ad4fe6b2a6aeed"
                          culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>
Run Code Online (Sandbox Code Playgroud)

(假设Newtonsoft.Json的程序集版本正好是6.0.0.0.)