c# – 错误单元测试webapi控制器

前端之家收集整理的这篇文章主要介绍了c# – 错误单元测试webapi控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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();

代码

controller.Request.SetConfiguration(new HttpConfiguration());

被执行我遇到异常

Message: Could not load file or assembly ‘Newtonsoft.Json,Version=4.5.0.0,Culture=neutral,PublicKeyToken=30ad4fe6b2a6aeed’ or one of its dependencies. The located assembly’s manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)

Source: System.Net.Http.Formatting

Stacktrace: at System.Net.Http.Formatting.JsonMediaTypeFormatter..ctor()
at System.Net.Http.Formatting.MediaTypeFormatterCollection.CreateDefaultFormatters()
at System.Net.Http.Formatting.MediaTypeFormatterCollection..ctor()
at System.Web.Http.HttpConfiguration.DefaultFormatters()
at System.Web.Http.HttpConfiguration..ctor(HttpRouteCollection routes)
at System.Web.Http.HttpConfiguration..ctor()
at EMR.Test.Controller.AccountControllerTest.Should_Get() in c:\PremiumProjectsCollection\emr\src\EMRAzure\EMRAzure\EMR.Test\Controller\AccountControllerTest.cs:line 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>

使用的测试跑步者是MStest,VS2012

解决方法

您需要添加一个 assembly redirect
<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>

(假设Newtonsoft.Json的装配版本正好是6.0.0.0)

原文链接:https://www.f2er.com/csharp/95850.html

猜你在找的C#相关文章