测试带有函数参数的函数时出现MissingMethodException

Bli*_*sco 4 f# nunit missingmethodexception fsunit

我正在使用FsUnit 2.1(使用NUnit 3.2)为F#项目编写测试.这是一个简单的模块:

namespace Library1
module LibraryFunctions =
    let Execute f1 = f1()
    let Id x = x
Run Code Online (Sandbox Code Playgroud)

这是我的测试:

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
Run Code Online (Sandbox Code Playgroud)

第二次测试失败(在NCrunch和ReSharper中),并显示以下消息:

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

如果我将测试模块放在与测试相同的代码文件中(而不是在单独的VS项目中),则测试通过.我怀疑这是由于NUnit和F#/ C#interop的一些问题.如果是这样,怎么解决?

Mar*_*son 5

这是FsUnit和其他项目的已知问题(请参阅此处此处).

作为解决方法,您可以将其添加到app.config文件中:

<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>
Run Code Online (Sandbox Code Playgroud)

注意:您需要更新FsUnit程序集正在使用的4.3.0.0任何版本FSharp.Core,以及何时更新.