0

我正在尝试编写自定义等式约束来比较 2 个对象。

open FsUnit
open NUnit.Framework.Constraints

type equalCompany(expected:Company) = 
    inherit Constraints.EqualConstraint(expected)    

    override this.ApplyTo (actual:Company option) = 
        //actual.IsSome |> should equal True
        //actual.Value.Id |> should equal expected.Id

        ConstraintResult(this, actual, true)

// example of usage:
actualCompany |> should equalCompany expectedCompany

它抱怨是因为 ApplyTo 实现匹配多个重载,我找不到正确的语法。
理想情况下,我喜欢与公司选项进行比较,但仍然只是公司很好。

涉及的类型如下:

type CompanyType =
    | Bank
    | Exchange

type Company = {
    Id: string
    Types: CompanyType list
}

并且我正在尝试编写我的等式约束,因为简单的现有equal不能与 Types 正常工作(列表,如果已排序,也总是不同)

如何正确覆盖ApplyTo函数?

4

1 回答 1

2

我认为问题ApplyTo在于您尝试覆盖的方法是通用的,需要有一个签名ApplyTo<'T> : 'T -> ConstraintResult

如果我正确理解您的代码,则您正在尝试定义 和 之间的Company比较Company option。为此,您需要(在运行时)检查传递给的值ApplyTo是否属于正确的类型。然后你可以转换它并实现你需要的任何逻辑。

这是一个对我有用的最小示例,编写为 F# 脚本文件:

#r "nuget: nunit"
#r "nuget: fsunit"

type Company(s) = 
  member x.Name = s

open FsUnit
open NUnit.Framework.Constraints

type equalCompany(expected:Company) = 
    inherit EqualConstraint(expected)    

    override this.ApplyTo<'T>(actual:'T) = 
        match box actual with 
        | :? option<Company> as co ->
          ConstraintResult(this, actual, 
            co.IsSome && co.Value.Name = expected.Name)
        | _ ->
          ConstraintResult(this, actual, false)

let actualCompany = Company("Test")
let expectedCompany = Company("Test")

// This passes, because it has the right type
Some actualCompany |> should equalCompany expectedCompany

// This does not, because the 'ApplyTo' logic does not handle this type
actualCompany |> should equalCompany expectedCompany
于 2021-10-06T10:32:12.807 回答