测试一个带有函数参数的函数时出现MissingMethodException异常

4

我正在使用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:未找到方法:'!!0 Library1.LibraryFunctions.Execute(Microsoft.FSharp.Core.FSharpFunc`2<Microsoft.FSharp.Core.Unit,!!0>)'
如果我将被测试的模块放在与测试相同的代码文件中(而不是单独的 VS 项目中),则测试通过。我怀疑这是由于 NUnit 和 F# / C# 交互的某些问题引起的。如果是这样,该如何解决?

1
你能检查一下这两个项目中 F# (Core) 的版本吗? - CaringDev
尝试将它们设置为相同的版本。这是版本不匹配经常遇到的问题。 - CaringDev
在两个项目中安装了最新的Nuget可用版本FSharp.Core 4.0.0.1。现在所有测试都失败并显示消息“System.MissingMethodException:找不到方法:'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2 < !! 0,!! 1>,!! 0,System.Object)'”。降级到3.1.2.5似乎可以解决问题 - 但我宁愿不这样做。 - Blisco
1
我没有可用的集成开发环境,但如果我没记错的话,您需要在配置文件中添加程序集绑定重定向。请搜索适当的设置。 - Mark Pattison
FsUnit可能是一个误导:我尝试剔除它,改用NUnit的Assert.AreEqual,但与我的原始问题一样出现了相同的问题。 - Blisco
显示剩余4条评论
1个回答

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>

注意:您需要更新4.3.0.0到与您的FsUnit程序集使用的FSharp.Core版本相匹配,以便在更新时使用。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接