I'm trying to write custom equality constraint to compare 2 objects.
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
It complains because the ApplyTo implementation matches multiple overloads and I can't find the right syntax.
Ideally I like to compare to Company option but still just Company is fine.
The types involved are the following:
type CompanyType =
| Bank
| Exchange
type Company = {
Id: string
Types: CompanyType list
}
and I'm trying to write my equality constraint because the simple existing equal does not work properly with Types (the list, also if sorted, appears always different)
How can I properly override the ApplyTo function?
I think the issue is that the
ApplyTomethod that you are trying to override is generic and needs to have a signatureApplyTo<'T> : 'T -> ConstraintResult.If I understand your code correctly, you are trying to define a comparison between
CompanyandCompany option. To Do this, you would need to check (at runtime) that the value passed toApplyTois of the right type. Then you can cast it and implement whatever logic you need.Here is a minimal sample that worked for me, written as an F# script file: