测试带有函数参数的函数时,缺少MissingMethodException [英] MissingMethodException when testing a function that takes a function parameter

查看:163
本文介绍了测试带有函数参数的函数时,缺少MissingMethodException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是我的测试:

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中),并显示以下消息:

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>)'.

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

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?

推荐答案

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

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

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

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>

注意:您需要将FsUnit程序集所使用的FSharp.Core版本更新为4.3.0.0,以及更新的时间.

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.

这篇关于测试带有函数参数的函数时,缺少MissingMethodException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆