【问题标题】:Mocked Interface with NSubstitute on F# does not allow ReturnsF# 上带有 NSubstitute 的模拟接口不允许返回
【发布时间】:2015-02-25 01:21:34
【问题描述】:

我有以下代码:

open NSubstitute
type MyClass()=
    let myObject = Substitute.For<IMyInterface>()
    do myObject.MyProperty.Returns(true)
    do myObject.MyMethod().Returns(true)

在“返回”(两者)我得到未定义的错误。等效的 C# 代码可以正常工作。在 do 行的末尾添加 |&gt; ignore 没有帮助。我错过了什么吗?

【问题讨论】:

    标签: f# c#-to-f# nsubstitute


    【解决方案1】:

    我知道您写道,在末尾添加 |&gt; ignore 并没有帮助,但这可以在我的机器上编译:

    type IMyInterface =
        abstract MyProperty : bool with get, set
        abstract member MyMethod : unit -> bool
    
    open NSubstitute
    
    type MyClass() =
        let myObject = Substitute.For<IMyInterface>()
        do myObject.MyProperty.Returns(true) |> ignore
        do myObject.MyMethod().Returns(true) |> ignore
    

    我试图从问题中推断出IMyInterface 的定义;我弄错了吗?

    【讨论】:

    • IMyInterface 是我的代码中的 C# 类。我尝试了一个全新的项目并复制了您在此处的代码,但仍然给我同样的错误。
    • 所以错误可能是我的 VS 安装中的环境问题。
    【解决方案2】:

    所以在我通过使用 fsc 和 VS2013 直接编译在 VS2012、fsi 上尝试了代码(Mark Seemann's,以避免我的 C# 类)之后,问题显然是 VS2012 问题。该代码与其他三个一起正常工作。

    尚不确定它是否使用了不同版本的 F#。将需要进一步调查。但至少,就目前而言,我可以在 VS2012 上使用:

    do SubstituteExtensions.Returns(myObject.MyProperty, true) |> ignore
    

    当我需要将参数传递给方法时,它也能正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-15
      • 1970-01-01
      • 2021-11-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多