Autofixture and Moq v4
Asked Answered
M

2

13

I installed Autofixture and Moq using Nuget.So I have moq version 4.

When running the following code

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

the following error shows up

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

I've also tried redirected it to the v4,but with no luck.

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

What could be the problem here ?

Mantooth answered 28/6, 2011 at 11:19 Comment(2)
See also this ticket: autofixture.codeplex.com/workitem/4225Goldagoldarina
As of version 2.13.1 the necessary assembly binding redirects are now automatically added to the config file when you install the AutoFixture.AutoMoq NuGet package.Lomeli
L
18

In order to redirect an assembly binding in a configuration file, you need to specify the urn:schemas-microsoft-com:asm.v1 namespace in the <assemblyBinding> element, like in this example:

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

It's interesting to point out that library assemblies compiled with an earlier version of .NET (such as Moq 3 and AutoFixture 2.1) will automatically be loaded in a process running on .NET 4.0 because of In-Process Side-by-Side execution. Here's a quote from MSDN about this:

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

Related resources:

Lomeli answered 28/6, 2011 at 11:55 Comment(0)
B
1

Accepted answer did not work for me since I want to use Moq 4 from the same class library (non exe) as AutoFixture.AutoMoq. There also seem to be no plans to support Moq 4 from the AutoFixture. I ended up using AutoFixture.AutoRhinoMocks as the next most popular choice, which does not conflict with Moq 4.

Bratton answered 26/9, 2013 at 19:31 Comment(1)
What problems are you seeing? I've been using AutoFixture.AutoMoq with Moq 4 for years, without problems.Leo

© 2022 - 2024 — McMap. All rights reserved.