我正在使用 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 和其他项目的一个已知问题(参见 here 和 here )。
作为一种解决方法,您可以将其添加到您的 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
到 FSharp.Core
的任何版本您的 FsUnit 程序集在更新时正在使用。
关于f# - 测试带有函数参数的函数时出现 MissingMethodException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36238316/