【问题标题】:F#'s Scaffold and FsUnit/FsCheckF# 的 Scaffold 和 FsUnit/FsCheck
【发布时间】:2015-04-21 20:54:27
【问题描述】:

我正在努力使用 ProjectScaffold 在 Linux 上开始使用 F#。 具体来说:我无法让项目与 FsUnit/FsCheck/xunit 一起使用。一世 有 F# 3.1 和 mono 3.12.1,我在 Linux (Ubuntu) x64 上。

我开始“我的项目”:

$ git clone --depth=1 git@github.com:fsprojects/ProjectScaffold.git
$ cd ProjectScaffold && ./build.sh

然后我在“src/MyProject/Library.fs”中添加一点代码:

module MyProject.X

let four = 4

然后对“tests/MyProject.Tests/Tests.fs”进行两次测试:

module MyProject.Tests.X

open Xunit
open FsUnit.Xunit
open FsCheck
open FsCheck.Xunit
open MyProject.X

[<Fact>]
let ``Two plus two is four.`` () =
    2 + 2 |> should equal four

[<Property>]
let ``Sorting a sorted list is idempotent.`` (l: int list) =
    let s = List.sort l
    s = List.sort s

此代码适用于我手动添加 FsCheck、FsUnit、anx xunit 1.9.2 的 Visual Studio(它在更高版本中失败 由于某些原因)。我在 Windows/Visual Studio 上的测试项目有这个配置文件:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="FsCheck" version="1.0.4" targetFramework="net45" />
  <package id="FsCheck.Xunit" version="1.0.4" targetFramework="net45" />
  <package id="FsUnit.xUnit" version="1.3.0.1" targetFramework="net45" />
  <package id="xunit" version="1.9.2" targetFramework="net45" />
  <package id="xunit.runner.visualstudio" version="2.0.0" targetFramework="net45" />
</packages>

所以我编辑 paket.dependencies 以添加这些包并删除 Nunit:

source https://nuget.org/api/v2

nuget FSharp.Formatting 2.8.0
nuget FSharpVSPowerTools.Core 1.7.0
nuget FAKE
nuget FsCheck 1.0.4
nuget FsCheck.Xunit 1.0.4
nuget FsUnit.xUnit 1.3.0.1
nuget xunit 1.9.2
nuget SourceLink.Fake

github fsharp/FAKE modules/Octokit/Octokit.fsx

然后:

$ mono .paket/paket.exe install

...它失败了,因为 NUnit 在某处被引用,所以我删除了 tests/MyProject.Tests/paket.references 中的引用和

$ mono .paket/paket.exe install

有效,但是

$ ./build.sh

失败,因为它找不到对 FsCheck 等人的引用。所以我假设我需要手动添加引用,所以 tests/MyProject.Tests/paket.references 现在是:

FsCheck
FsCheck.Xunit
FsUnit.xUnit
xunit

...构建的 build.sh 再次失败:它找不到 FsCheck。我在 paket doc 中找不到如何添加本地依赖项(MyProject.Tests 应该引用 MyProject),它可能会自动完成。

【问题讨论】:

  • 决定是使用 NuGet(并在 VS 中添加引用)还是 paket,不要像这样混合使用它们!保留您的 paket.references,删除所有 nuget 包和引用(从 Visual Studio 中)并运行 paket install(这应该为您重新添加所有引用)。项目引用不受 paket 影响,因此只需像往常一样通过 Visual Studio 添加它们。

标签: f# mono


【解决方案1】:

我遇到了这个问题,我花了一段时间才找到解决办法。对于 Tests 目录中的项目文件,我不得不更改:

<Reference Include="FsUnit.NUnit">
    <HintPath>..\..\packages\FsUnit.1.3.0.1\Lib\Net40\FsUnit.NUnit.dll</HintPath>
    <Private>True</Private>
</Reference>

到这里:

<Reference Include="FsUnit.NUnit">
    <HintPath>..\..\packages\FsUnit\Lib\Net40\FsUnit.NUnit.dll</HintPath>
    <Private>True</Private>
</Reference>

同样,对于 NUnit:

<Reference Include="nunit.framework">
  <HintPath>..\..\packages\NUnit.2.6.3\lib\nunit.framework.dll</HintPath>
  <Private>True</Private>
</Reference>

收件人:

<Reference Include="nunit.framework">
  <HintPath>..\..\packages\NUnit\lib\nunit.framework.dll</HintPath>
  <Private>True</Private>
</Reference>

问题在于,在单声道上,包在路径中没有版本,但在 Visual Studio 下却有。找到此修复程序后,我为测试创建了两个 .fsproj 文件,并修改了 build.sh 脚本以在单声道下交换单声道兼容的文件:

#!/bin/bash
if test "$OS" = "Windows_NT"
then
  # no changes in here
else
  # fix test fsproj file
  mv tests/ProjectName.Tests/ProjectName.Tests.fsproj tests/ProjectName.Tests/ProjectName.Tests.fsproj.vs
  mv tests/ProjectName.Tests/ProjectName.Tests.fsproj.mono tests/ProjectName.Tests/ProjectName.Tests.fsproj  

  # leave the script logic for mono in place

  # put project files back to avoid git noticing the swap
  mv tests/ProjectName.Tests/ProjectName.Tests.fsproj tests/ProjectName.Tests/ProjectName.Tests.fsproj.mono
  mv tests/ProjectName.Tests/ProjectName.Tests.fsproj.vs tests/ProjectName.Tests/ProjectName.Tests.fsproj

fi

一旦我进行了这些更改,该项目在 Visual Studio 和单声道下都能正常工作。

【讨论】:

  • 感谢您的回答,但这似乎不是问题所在。项目文件确实有 dll 的正确引用,我不知道为什么我得到“未定义命名空间或模块 'FsCheck'”。
【解决方案2】:

我不太确定,我了解您:您是否有两个 fsproj 文件,一个用于生产代码,一个用于单元测试?您是否在 fsproj 中引用了 FsCheck?

另外,我记得一个问题,如果您针对较旧版本的 .net 进行编译并引用针对较新版本的 .net 编译的程序集,它将表现得好像没有引用一样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多