question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow using an IEqualityComparer in BeEquivalentTo

See original GitHub issue

Actually, regarding my suggestion, it would also work if you could supply an EqualityComparer for EquivalencyAssertionOptions, or is it already possible? I’m just saying because EqualityComparer is the “standard” .NET way of doing it, and often there is already a suitable equality comparer available that could be reused as is. At least in our case, I like writing EqualityComparers because they nicely encapsulate the equivalency. I don’t like repeating equivalency rules in-line, because usually the same rules are needed in many places. I guess you could encapsulate them as IEquivalencySteps, but why do that if you already have EqualityComparer?

For example

orderDto.Should().BeEquivalentTo(order, options => options
    .Using<DateTimeEqualityComparer>());

or

orderDto.Should().BeEquivalentTo(order, options => options
    .Using<DateTime>(new DateTimeEqualityComparer()));

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
codewisdomcommented, Jan 15, 2020

I really like this idea! Agree with @jnyrup that the second is more generic and clearer. I’ve been struggling with comparing types in a homegrown library to C# types. Using the DifferentObjectsEquivalencyStep approach I found on SO has been successful up to a point.

2reactions
jnyrupcommented, Feb 15, 2020

I like the second one the most. It’s explicit and generic.

public static void Using<T, TComparer>()
    where TComparer : IEqualityComparer<T>
{
}

or

public static void Using<T>(IEqualityComparer<T> comparer)
{
}

I’m not sure what the signature of the first one would be?

public static void Using<TComparer>()
    where TComparer : IEqualityComparer
{
}

I dislike that as:

  • It’s non-generic in the type being compared, I often (if not always) only implement the generic IEqualityComparer<T> and not the non-generic IEqualityComparer
  • It not visible from the test which types are being compared using the comparer.

In general I’d like to take Fluent Assertions to a more generic world.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use a specific comparer func for Equality in ...
1 Answer 1 ... Why don't you replace the call to BeEquivalentTo with a custom assertion built on top of Execute.Assertion as documented...
Read more >
Object graph comparison
BeEquivalentTo () compares Enum members by the enum's underlying numeric value. An option to compare an Enum only by name is also available,...
Read more >
Fluent Assertions 6.0, the biggest release ever
And for the more complicated cases, you can use NotContainEquivalentOf to assert the absence of an element in the collection using the same ......
Read more >
Releases - Fluent Assertions
NotContainMatch() to assert that the collection does not contain a string that matches a wildcard pattern - #1246. The Using / When option...
Read more >
IEqualityComparer<T> Interface
This interface allows the implementation of customized equality comparison for collections. That is, you can create your own definition of equality for type...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found