System.MissingMethodException (Updating Foq to 1.4)


The following assertion passes with Foq 1.0, and Foq 1.3, but fails when updating to Foq 1.4:
let mock = Mock<IMemberComparer>().Create()
Assert.IsAssignableFrom<IMemberComparer>(mock) |> ignore
The exception is:
System.MissingMethodException : Method not found: 'Void Foq.Mock`1..ctor(Microsoft.FSharp.Core.FSharpOption`1<Microsoft.FSharp.Core.FSharpFunc`2<System.Type,System.Object>>)'.
The IMemberComparer interface in the assertion can be found here.
Closed Jan 26, 2014 at 6:33 PM by ptrelford


ptrelford wrote Dec 4, 2013 at 2:03 PM

This is either the same issue with app.config or you are trying to invoke the Mock constructor via reflection and in Foq 1.4 the constructor now takes a (default) argument.
If this is an issue I can re-release and use overloads instead of default arguments.

baxevanis wrote Dec 4, 2013 at 8:25 PM

Hm... Why should Mock<IMemberComparer>().Create() invoke the Mock constructor via reflection?

Shouldn't all passing unit tests in 1.0, and 1.3, also pass with 1.4? By unit tests I mean external code (user code, consumer code, etc).

ptrelford wrote Dec 4, 2013 at 8:44 PM

Sorry, just take a few guesses, reason for issue isn't immediately obvious, will investigate further.

ptrelford wrote Dec 4, 2013 at 9:00 PM

I've set up the scenario (C# library with interface as per the snippet/F# library test project) as per your description in Visual Studio 2012 with both targeting .Net 4.0, and pulling Foq 1.4 directly from Nuget.
The error I get is:
Test 'Tests.test' failed: 
  Expected: assignable from <Ploeh.SemanticComparison.IMemberComparer>
  But was:  <Mock.IMemberComparera4122560-43a9-43f3-8075-659c088af6e2>
    c:\users\phil\documents\visual studio 2012\Projects\ClassLibrary3\Library1\Library1.fs(10,0): at Tests.test()
To reproduce the issue (MethodMissingException) I have to explicitly remove the app.config that Nuget adds when I install the Foq 1.4 package. It is possible that Nuget fails to add or change the app.config file if the files are read-only, if so you may need to specify the bindingRedirect attributes in the app.config file manually, i.e.
<?xml version="1.0" encoding="utf-8"?>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="" newVersion="" />