繁体   English   中英

F#如何设置可以使用FsUnit的FAKE项目

[英]F# How to setup FAKE project that can use FsUnit

我正在尝试设置一个可以运行FsUnit的基本FAKE F#项目,但我无法弄清楚如何解决Method not found: 'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2<!!0,!!1>, !!0, System.Object)'Method not found: 'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2<!!0,!!1>, !!0, System.Object)'错误。

我已经阅读了以下似乎相关的帖子,但我显然仍然不喜欢它:

我已经使用以下设置创建了一个JunkTest库项目:

paket.dependencies

source https://www.nuget.org/api/v2
nuget FAKE
nuget FSharp.Core
nuget FsUnit
nuget NUnit
nuget NUnit.Console

paket.references

FSharp.Core
FsUnit
NUnit

JunkTest.fs

module JunkTest

open FsUnit
open NUnit.Framework

[<Test>]
let ``Example Test`` () =
    1 |> should equal 1               // this does not work
    //Assert.That(1, Is.EqualTo(1))   // this works (NUnit)

build.fsx(相关部分)

Target "Test" (fun _ ->
    !! (buildDir + "JunkTest.dll")
    |> NUnit3 (fun p ->
        {p with OutputDir = "TestResults" }
    )
)

产量

我看到正在从本地packages目录Copying file from "c:\\Users\\dangets\\code\\exercism\\fsharp\\dgt\\packages\\FSharp.Core\\lib\\net40\\FSharp.Core.dll" to "c:\\Users\\dangets\\code\\exercism\\fsharp\\dgt\\build\\FSharp.Core.dll".

并且nunit3-console执行: c:\\Users\\dangets\\code\\exercism\\fsharp\\dgt\\packages\\NUnit.ConsoleRunner\\tools\\nunit3-console.exe "--noheader" "--output=TestResults" "c:\\Users\\dangets\\code\\exercism\\fsharp\\dgt\\build\\JunkTest.dll"

我尝试使用以下内容在测试项目根目录中添加app.config文件,但它似乎没有解决问题(注意我不使用Visual Studio - 我是否需要为项目执行任何特殊操作包含app.config文件?):

<?xml version="1.0" encoding="utf-8" ?>
<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.1.0" newVersion="4.3.1.0" />
        </dependentAssembly>
      </assemblyBinding>
    </runtime>
</configuration>

任何和所有的帮助表示赞赏。

编辑:解决方案是我没有正确设置App.config文件以包含在构建中。 所有答案都说“只是将它添加到你的App.config文件”对我没有帮助,因为VSCode没有自动将它添加到fsproj文件中。

我添加的部分是:

<None Include="App.config" />

在包含其他<Compile Include=Foo.fs>行的ItemGroup中。

这是因为FSharp.Core版本不匹配。 请参阅,您的应用程序引用了一个版本的FSharp.CoreFsUnit引用了另一个版本。 这意味着FSharpFunc<_,_>类型将是不同的(来自不同的组件来)为您和FsUnit ,这反过来又意味着, should按功能输出FsUnit是不是你的代码是寻找相同的功能,因为它有一个不同类型的参数。

这就是bindingRedirect用武之地。你完全正确地将它添加到app.config ,但是从你关于你是否正确地做到这一点的问题来看,我怀疑你可能没有。 app.config是,它实际上不是程序配置。 相反,它是程序配置的源代码 在编译时,此文件将被复制到bin\\Debug\\Your.Project.dll.config ,然后才会在运行时获取它。 如果你没有将这个文件添加到fsproj项目文件中(我怀疑可能是这种情况),那么在构建期间它不会被复制到正确的位置,因此不会在运行时被拾取。

它仍然无法正常工作的另一个原因可能是您在app.config文件中指定了错误版本的FSharp.Core 这让我想到了下一点。

手工制作该文件有点脆弱:当您将FSharp.Core升级到新版本(或者Paket为您完成)时,您可能忘记在app.config修复它,即使您没有,也可以麻烦。 但Paket可以为您提供帮助:如果您redirects: on paket.dependencies文件中添加redirects: on选项 ,Paket会自动将bindingRedirect cruft添加到您的app.config

source https://www.nuget.org/api/v2
nuget FAKE
nuget FSharp.Core redirects: on
nuget FsUnit
nuget NUnit
nuget NUnit.Console

这听起来像FSharp.Core版本不匹配。

你正在使用的NuGet软件包随附FSharp.Core 4.4(不是4.3.1)。 我建议修改绑定重定向使用4.4:

<bindingRedirect oldVersion="0.0.0.0-4.3.1.0" newVersion="4.4.0.0" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM