Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make FluentAssertions ShouldBeEquivalentTo check for type when comparing?

I have 2 dictionaries and I would expect the contents not to be equivalent as the dictionary contains values of different types. However the following test passes

[Scenario]
public void DictionariesWithDifferentTypesShouldBeEquivalent(
    Dictionary<string, object> firstDictionary, 
    Dictionary<string, object> secondDictionary)
{
    "Given a dictionary"
        .f(() => firstDictionary = new Dictionary<string, object> 
                    {
                        { "latency", 0 },
                        { "errorMessages", new string[0] },
                        { "lastChanged", new DateTime(635272310930829706) },
                        { "query", new string[0] },
                        { "items", new string[] { "foo", "bar" } },
                        { "name", "Bob" },
                        { "number", 3 },
                        { "updateInterval", 10 },
                    });

    "And a second dictionary with same values but of differing types"
        .f(() => secondDictionary = new Dictionary<string, object> 
                    {
                        { "latency", 0L },
                        { "errorMessages", new object[0] },
                        { "lastChanged", new DateTime(635272310930829706) },
                        { "query", new string[0] },
                        { "items", new string[] { "bar", "foo" } },
                        { "name", "Bob" },
                        { "number", 3 },
                        { "updateInterval", "10" },
                    });

    "When I check for equivalency"
        .f(() => { });

    "Then the dictionaries should be equivalent"
        .f(() => firstDictionary.ShouldBeEquivalentTo(secondDictionary));
}

If this is the expected behaviour how can I set up a fluent assertions rule to check that the type matches?

I have investigated using both a MatchingRule and an AssertionRule but in both cases I dont seem to have access to the original types of the subject and expected. It appears the the subject has already been converted to the type of the expected. I.e in the exapmle above updateInterval in the first dictionary would already have been converted to a string for comparison with the second dictionary.

Thanks for the help,
Rachael

like image 574
Rachael Avatar asked Nov 10 '22 06:11

Rachael


1 Answers

But they are equivalent. Both dictionaries contain the same keys and values that are deemed to be equivalent. 0L and 0 can be converted to the same type and therefor are equivalent. And for the record, ShouldBeEquivalentTo doesn't do a reference equality check persee. But if the subject and expectation are the same object, then yes, it can safely assume they are equivalent as well.

like image 186
Dennis Doomen Avatar answered Nov 14 '22 23:11

Dennis Doomen