1

I'm trying to replace usage of "CollectionAssert.AreEquivalent()" with FluentAssertion".

I've tried using ShouldAllBeEquivalentTo, but the function doesn't fail when comparing similar objects of different types.

In the example below, both calls succeed. I want the 2nd one to fail.

new int[] { 1, 2 }.ShouldAllBeEquivalentTo( new int[] { 2, 1 } );       
new int[] { 1, 2 }.ShouldAllBeEquivalentTo( new string[] {"1", "2"} );  

Is there an alterantive function or a certain option which would make the 2nd line fail?

FrequentGuest
  • 182
  • 2
  • 11
  • what about adding this assert: Assert.AreEqual(list1.GetType(), list2.GetType()); ? – elirandav Feb 05 '17 at 16:46
  • @KernelMode - this would work for that specific example, but my actual case involves a dictionary. I need to check runtime types of each individual value. CollectionAssert does this, as well as FluentAssertions after adding the step detailed in the accepted answer – FrequentGuest Feb 06 '17 at 08:09

1 Answers1

2

That's because by default the TryConversionEquivalencyStep is used and it will treat both "1" and 1 as equal (after attempted conversion).

Try to remove it first:

AssertionOptions.EquivalencySteps.Remove<TryConversionEquivalencyStep>();

See Source

haim770
  • 48,394
  • 7
  • 105
  • 133
  • Thanks for the answer! After reading more about it, seems like this is going to be the default in FluentAssertions 5 - [relevant discussion](https://github.com/dennisdoomen/FluentAssertions/issues/472). – FrequentGuest Feb 06 '17 at 08:12