.net - 自动夹具和最小起订量 v4

标签 .net unit-testing moq autofixture

我使用 Nuget 安装了 Autofixture 和 Moq。所以我有 moq 版本 4。

运行以下代码时

var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.CreateAnonymous<ISomething>();

出现以下错误

System.IO.FileLoadException : Could not load file or assembly 'Moq, Version=3.1.416.3, Culture=neutral, PublicKeyToken=69f491c39445e920'



我也尝试将其重定向到 v4,但没有运气。
<configuration>
  <runtime>
    <assemblyBinding>
    <dependentAssembly>
      <assemblyIdentity name="Moq" publicKeyToken="69f491c39445e920" culture="neutral"/>
      <bindingRedirect oldVersion="3.1.416.3" newVersion="4.0.10827.0"/>
    </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

这里可能是什么问题?

最佳答案

为了重定向配置文件中的程序集绑定(bind),您需要指定 urn:schemas-microsoft-com:asm.v1 <assemblyBinding> element 中的命名空间,就像在这个例子中:

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Moq"
                                  publicKeyToken="69f491c39445e920"
                                  culture="neutral"/>
                <bindingRedirect oldVersion="3.1.416.3"
                                 newVersion="4.0.10827.0"/>
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

有趣的是,使用早期版本的 .NET 编译的库程序集(例如 Moq 3AutoFixture 2.1 )将自动加载到在 .NET 4.0 上运行的进程中,因为 In-Process Side-by-Side execution .这是来自 MSDN 的关于此的引用:

If an application is compiled using the .NET Framework 4 runtime but includes a library that was built using an earlier runtime, that library will use the .NET Framework 4 runtime as well. However, if you have an application that was built using an earlier runtime and a library that was built using the .NET Framework 4, you must force your application to also use the .NET Framework 4



相关资源:
  • Redirecting Assembly Versions
  • 关于.net - 自动夹具和最小起订量 v4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6505591/

    相关文章:

    multithreading - 如何测试在 goroutine 中监视文件的函数

    scala - 如何让 SBT 仅重新运行失败的测试

    c# - 仅实现单个枚举的最佳方法 Moq IEnumerable

    c# - 为什么 "-less"在 "hello"之后而不是在它之前排序?

    c# - 有没有办法在用户单击另一行中的单元格时保持选中 DataGridView 行?

    C# IEnumerator/yield 结构可能不好?

    android - 为单元测试初始化​​ Activity

    c# - 使用 Moq 和 MSTest 测试异常的正确方法

    c# - 如何使用 Moq 模拟使属性在设置时抛出异常?

    .NET 部署到网络共享