gpt4 book ai didi

c# - 错误单元测试 webapi Controller

转载 作者:可可西里 更新时间:2023-11-01 08:45:32 24 4
gpt4 key购买 nike

我正在使用 AspNet Web Api Client 5.0,我正在尝试对 Web API Controller 进行单元测试。

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。)

关于c# - 错误单元测试 webapi Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22164669/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com