Autofixture和Moq v4

13
我使用Nuget安装了Autofixture和Moq。因此,我有Moq的4版本。运行以下代码时:
var fixture = new Fixture().Customize(new AutoMoqCustomization());
fixture.CreateAnonymous<ISomething>();

出现以下错误:

System.IO.FileLoadException : 无法加载文件或程序集'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>

这里可能出了什么问题?


1
请参见此工单:http://autofixture.codeplex.com/workitem/4225 - svrist
版本2.13.1开始,当您安装AutoFixture.AutoMoq NuGet包时,必要的程序集绑定重定向现在会自动添加到配置文件中 - Enrico Campidoglio
2个回答

18
为了在配置文件中重定向程序集绑定,您需要在 <assemblyBinding> 元素中指定 urn:schemas-microsoft-com:asm.v1 命名空间,就像这个例子中一样:
<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上运行的进程中。以下是MSDN关于此的引用:

如果一个应用程序是使用.NET Framework 4运行时编译的,但包含了使用早期运行时构建的库,那么该库也将使用.NET Framework 4运行时。然而,如果您有一个使用早期运行时构建的应用程序和一个使用.NET Framework 4构建的库,那么您必须强制使您的应用程序也使用.NET Framework 4。

相关资源:

1

采纳的答案对我无效,因为我想从同一类库(非exe)中使用Moq 4,就像AutoFixture.AutoMoq一样。似乎也没有计划支持AutoFixture的Moq 4版本。最终我选择了次受欢迎的选择AutoFixture.AutoRhinoMocks,它不会与Moq 4发生冲突。


3
你看到了哪些问题?我多年来一直使用 AutoFixture.AutoMoq 和 Moq 4,没有遇到任何问题。 - Mark Seemann

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接