c# - Moq 4.13.1 与 Task.Extensions 不兼容

标签 c# .net moq

我正在尝试运行使用 Moq 的单元测试。问题是我一直在使用 Visual Studio 2019。

Message: System.TypeInitializationException : The type initializer for 'Moq.DefaultValueProvider' threw an exception. ----> System.IO.FileLoadException : Could not load file or assembly 'System.Threading.Tasks.Extensions, Version=4.2.0.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)



我正在使用最新版本的 Moq 4.13.1 和 System.Threading.Tasks.Extensions v.4.5.3
是否有任何特定版本的 System.Threading.Tasks.Extensions 或 .NET 应该与 Moq 一起使用?

最佳答案

您可以将应用程序配置文件添加到您的测试项目 (App.config) 并执行绑定(bind)重定向到当前安装的 System.Threading.Tasks.Extensions 版本,例如

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="System.Threading.Tasks.Extensions" publicKeyToken="cc7b13ffcd2ddd51" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.2.0.1" newVersion="4.2.0.1" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

关于c# - Moq 4.13.1 与 Task.Extensions 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60217316/

相关文章:

C# ApiController 返回列表但 postman 显示 "Could not get any response"

c# - 尝试调用 function<T> where T : TheClass, new() where T: class

c# - EF将重复记录添加到查找/引用表中

c# - 当我只知道祖先类的类型时,如何反序列化 XML?

c# - 代码合约静态分析: Prover Limitations?

c# - 如何在 Environment.Exit() 之前调用事件?

c# - 将项目添加到 ComboBox

c# - 使用 Moq 时在 Dapper 方法上获取 NotSupportedException

moq - 新手尝试将 Moq 用于可枚举方法

c# - 单元测试 unityContainer.Resolve