4

I am using FsUnit 2.1 (with NUnit 3.2) to write tests for an F# project. Here is a simple module:

namespace Library1
module LibraryFunctions =
    let Execute f1 = f1()
    let Id x = x

And here are my tests:

namespace Tests
open FsUnit
open NUnit.Framework
open Library1

[<TestFixture>]
type Tests() =

    [<Test>]
    // Passes
    member x.``LibraryFunctions.Id should return the value``() =
        LibraryFunctions.Id 42 |> should equal 42

    [<Test>]
    // Fails
    member x.``LibraryFunctions.Execute should return the result of the function``() =
        let f() = 42
        LibraryFunctions.Execute f |> should equal 42

The second test fails (in NCrunch and ReSharper) with the message:

System.MissingMethodException : Method not found: '!!0 Library1.LibraryFunctions.Execute(Microsoft.FSharp.Core.FSharpFunc`2<Microsoft.FSharp.Core.Unit,!!0>)'.

If I put the module under test in the same code file as the tests (rather than in a separate VS project) the test passes. My suspicion is that this is due to some issue with NUnit and F#/C# interop. If so, how can it be resolved?

Guy Coder
  • 24,501
  • 8
  • 71
  • 136
Blisco
  • 601
  • 6
  • 14
  • 1
    Could you check F# (Core) versions for both projects? – CaringDev Mar 26 '16 at 17:30
  • Both test are passing form me. I have `Library1` in one project with F# core: 4.3.1.0 Then I have 'Testes" in another project with FsUnit: 2.1, NUnit: 3.2 and F# Core: 3.1.2.5 Specific Version: False. I am using VS 2015. I do not have NCrunch and ReSharper. The test are being run within VS using Test Explorer with NUnit VS Adapter installed. – Guy Coder Mar 26 '16 at 18:07
  • @CaringDev `Library1` is using FSharp.Core 4.4.0.0, `Tests` is using 3.1.2.5 with Specific Version: false. – Blisco Mar 26 '16 at 18:53
  • Try setting them to the same version. This is an often encountered problem with versions mismatches. – CaringDev Mar 26 '16 at 19:11
  • Installed FSharp.Core 4.0.0.1 (the latest available on Nuget) in both projects. Now all tests fail with the message ``System.MissingMethodException : Method not found: 'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2<!!0,!!1>, !!0, System.Object)'``. Downgrading to 3.1.2.5 seems to work - but I'd rather not do that. – Blisco Mar 26 '16 at 20:50
  • Did you try to also re-install FsUnit? Might be the wrong version for 4.x – CaringDev Mar 26 '16 at 20:56
  • 1
    I don't have an IDE available, but IIRC you have to add assembly binding redirects to your config file. Google for the appropriate settings. – Mark Pattison Mar 26 '16 at 21:18
  • FsUnit may be a red herring: I tried cutting it out and using NUnit's `Assert.AreEqual` instead, and the same issue occurs as in my original question. – Blisco Mar 26 '16 at 21:19
  • @MarkPattison Adding binding redirects worked. Thanks! – Blisco Mar 26 '16 at 21:24

1 Answers1

5

This is a known issue with FsUnit and other projects (see here and here).

As a workaround, you can add this to your app.config file:

<configuration>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="FSharp.Core" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-4.3.0.0" newVersion="4.3.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

NB: you'll need update4.3.0.0 to whatever version of FSharp.Core your FsUnit assembly is using, as and when it is updated.

Mark Pattison
  • 2,964
  • 1
  • 22
  • 42