简体   繁体   English

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

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

I'm trying to setup a basic FAKE F# project that can run FsUnit but I cannot figure out how to solve the Method not found: 'Void FsUnit.TopLevelOperators.should(Microsoft.FSharp.Core.FSharpFunc`2<!!0,!!1>, !!0, System.Object)' errors. 我正在尝试设置一个可以运行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)'错误。

I have read the following posts that seem to be related, but I'm apparently still not grokking it: 我已经阅读了以下似乎相关的帖子,但我显然仍然不喜欢它:

I have created a JunkTest library project with the following setup: 我已经使用以下设置创建了一个JunkTest库项目:

paket.dependencies paket.dependencies

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

paket.references paket.references

FSharp.Core
FsUnit
NUnit

JunkTest.fs 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 (relevant part) build.fsx(相关部分)

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

Output 产量

I see that FSharp.Core.dll is being copied from the local packages directory: 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". 我看到正在从本地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".

And the nunit3-console execution: 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" 并且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"

I have tried to add a app.config file with the in the test project root directory with the following but it doesn't seem to solve the issue (NOTE I am not using Visual Studio - do I need to do anything special for the project to include the app.config file?): 我尝试使用以下内容在测试项目根目录中添加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>

Any and all help is appreciated. 任何和所有的帮助表示赞赏。

EDIT: The solution was that I was not properly setting up the App.config file to get included in the build. 编辑:解决方案是我没有正确设置App.config文件以包含在构建中。 All of the answers that said "just add this to your App.config file" didn't help me because VSCode doesn't add this to the fsproj file automatically. 所有答案都说“只是将它添加到你的App.config文件”对我没有帮助,因为VSCode没有自动将它添加到fsproj文件中。

The part that I added is: 我添加的部分是:

<None Include="App.config" />

In the ItemGroup that contains the other <Compile Include=Foo.fs> lines. 在包含其他<Compile Include=Foo.fs>行的ItemGroup中。

This happens because of FSharp.Core version mismatch. 这是因为FSharp.Core版本不匹配。 See, your application references one version of FSharp.Core and FsUnit references another version. 请参阅,您的应用程序引用了一个版本的FSharp.CoreFsUnit引用了另一个版本。 This means that the FSharpFunc<_,_> type is going to be different (coming from different assemblies) for you and FsUnit , which in turn means that the should function exported by FsUnit is not the same function that your code is looking for, because it has a parameter of a different type. 这意味着FSharpFunc<_,_>类型将是不同的(来自不同的组件来)为您和FsUnit ,这反过来又意味着, should按功能输出FsUnit是不是你的代码是寻找相同的功能,因为它有一个不同类型的参数。

This is where the bindingRedirect comes in. You're absolutely correctly added it to app.config , but from your question about whether you're doing it correctly, I get a suspicion that you might not. 这就是bindingRedirect用武之地。你完全正确地将它添加到app.config ,但是从你关于你是否正确地做到这一点的问题来看,我怀疑你可能没有。 The thing with app.config is, it's not actually the program configuration. app.config是,它实际上不是程序配置。 Rather, it's the source code for program configuration. 相反,它是程序配置的源代码 At compile time, this file gets copied to bin\\Debug\\Your.Project.dll.config , and only then it will get picked up at runtime. 在编译时,此文件将被复制到bin\\Debug\\Your.Project.dll.config ,然后才会在运行时获取它。 If you didn't add this file to the fsproj project file (which, I suspect, might be the case), then it's not getting copied to the right place during build, and thus isn't getting picked up at runtime. 如果你没有将这个文件添加到fsproj项目文件中(我怀疑可能是这种情况),那么在构建期间它不会被复制到正确的位置,因此不会在运行时被拾取。

Another reason for it still not working may be that you've specified an incorrect version of FSharp.Core in your app.config file. 它仍然无法正常工作的另一个原因可能是您在app.config文件中指定了错误版本的FSharp.Core Which brings me to the next point. 这让我想到了下一点。

Crafting that file by hand is a bit fragile: when you upgrade FSharp.Core to a new version (or Paket does it for you), you may forget to fix it in app.config and even if you don't, it's a bit of a hassle. 手工制作该文件有点脆弱:当您将FSharp.Core升级到新版本(或者Paket为您完成)时,您可能忘记在app.config修复它,即使您没有,也可以麻烦。 But Paket can help you with that: if you add the redirects: on options to your paket.dependencies file, Paket will add the bindingRedirect cruft to your app.config automatically: 但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

This sounds like an FSharp.Core version mismatch. 这听起来像FSharp.Core版本不匹配。

The NuGet package you're using ships with FSharp.Core 4.4 (not 4.3.1). 你正在使用的NuGet软件包随附FSharp.Core 4.4(不是4.3.1)。 I recommend modifying your binding redirect to use 4.4: 我建议修改绑定重定向使用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