加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-15 06:19:57 所属栏目:百科 来源:网络整理
导读:我正在使用AspNet Web Api Client 5.0,我正在尝试单元测试一个web api控制器. var encservice = new EncryptionService();var acctservice = FakeServices.GetAccountService();var controller = new AccountController(acctservice,encservice);controller.
我正在使用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:PremiumProjectsCollectionemrsrcEMRAzureEMRAzureEMR.TestControllerAccountControllerTest.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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读