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

标签 f# nunit missingmethodexception fsunit

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

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

这是我的测试:
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

第二个测试失败(在 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# 互操作的一些问题。如果是这样,如何解决?

最佳答案

这是 FsUnit 和其他项目的一个已知问题(参见 herehere )。

作为一种解决方法,您可以将其添加到您的 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>

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

关于f# - 测试带有函数参数的函数时出现 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238316/

相关文章:

f# - 折叠还是选择直到无?

c# - 在不执行脚本的 nUnit 上运行 Selenium 测试

c# - .NET 测试框架建议

c# - 断言测试失败就是成功

f# - 为什么一个变量可以在一个模式中绑定(bind)两次?

c# - 引用 x64 dll 文件在 C# 中有效,但在 F# 中无效

sorting - 在 F# 中对元组内的值进行排序

grails - Grails集成测试中的奇怪行为

.net - 诊断 MissingMethodException

c# - 为什么无法捕获 MissingMethodException?