【问题标题】:FsUnit: Unable to test portable library due to it and test project having different F#.Core versionsFsUnit:无法测试可移植库,因为它和测试项目具有不同的 F#.Core 版本
【发布时间】:2016-09-21 19:34:08
【问题描述】:

我有一个便携式库,其FSharp.Core 版本是3.7.4.0。安装(在单元测试项目中)FsUnit 作为依赖项安装FSharp.Core 版本3.1.2.5

因此,在我的单元测试项目中使用可移植库的功能,例如:

module StammaTests.PieceTests

open Stamma
open NUnit.Framework
open FsUnitTyped

[<Test>]
let ``Testing a Basic function`` () =
    Piece.toChar Black King |> shouldEqual 'k'

产生错误:

结果消息:System.IO.FileLoadException:无法加载文件或程序集'FSharp.Core,版本=3.7.4.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。这 定位程序集的清单定义与程序集不匹配 参考。 (HRESULT 异常:0x80131040)

尝试将 FSharp.Core 版本从 NuGet 更新到 4.0.0.1(甚至在更新时检查两个项目),现在甚至是一些简单的东西,例如:

[<Test>]
let ``Testing the test`` () = 1 |> shouldEqual 1 

不起作用,出现类似的错误。

结果消息:System.IO.FileLoadException:无法加载文件或 程序集'FSharp.Core,版本=4.3.1.0,文化=中性, PublicKeyToken=b03f5f7f11d50a3a' 或其依赖项之一。这 定位程序集的清单定义与程序集不匹配 参考。 (HRESULT 异常:0x80131040)

并且第一次失败测试的错误没有改变。

我觉得我错过了一些非常明显的东西,我发现几个人有类似的问题,但我不明白他们做了什么来解决它(他们似乎都解决了它..)例如this one .

编辑

这两个项目都是库,我没有要添加任何内容的 app.config 文件。

【问题讨论】:

    标签: f# nunit fsunit


    【解决方案1】:

    在您的app.config 文件中添加绑定重定向,以将所有FSharp.Core 绑定重定向到您想要的版本。例如,要使用 4.4.0 版,您的 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.4.0.0" newVersion="4.4.0.0" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    【讨论】:

    • 我应该将此添加到哪个项目?
    • 明确一点,两个项目都是库,我没有app.config 文件。我在其他地方阅读了这个答案,但由于我没有文件,所以我不知道如何使用它。
    • 只需将新的 app.config 文件添加到您的库项目中
    • 无法正常工作。我将&lt;bindingRedirect oldVersion="0.0.0.0-4.4.0.0" newVersion="3.1.2.5" /&gt; 放入我的库中,rebuit 和同样的错误。新版本中的不同版本也没有解决。
    • 好吧,你没有说nunit.framework的绑定失败
    【解决方案2】:

    我找到了一个实际可行的解决方案here

    基本上,在 test 项目中添加一个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.1.0" newVersion="4.3.1.0" />
          </dependentAssembly>
          <dependentAssembly>
            <assemblyIdentity name="nunit.framework" publicKeyToken="96d09a1eb7f44a77" culture="neutral" />
            <bindingRedirect oldVersion="0.0.0.0-2.6.4.14350" newVersion="2.6.4.14350" />
          </dependentAssembly>
        </assemblyBinding>
      </runtime>
    </configuration>
    

    它为Fsharp.CoreNUnit.Framework 添加了两者的绑定,这与通常的解决方案不同,您只为Fsharp.Core 添加绑定。

    【讨论】:

    • 所以,暗示似乎是 FSharp.Core 引用了 nunit.framework。是这样吗?我问是因为它看起来很奇怪。
    • 我不这么认为。我只把对我有用的东西放在这里。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多