How can I do FluentAssertions ShouldBeEquivalent to check type when comparing?

I have 2 dictionaries, and I expect the content to not be equivalent, since the dictionary contains values ​​of different types. However, the next 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 behavior, how can I set up a free claims rule to check if the type matches?

I explored the use of both MatchingRule and AssertionRule, but in both cases I don't seem to have access to the original object types and expected ones. Apparently, the object has already been converted to the expected type. Ie in exapmle above updateInterval in the first dictionary would already be converted to a string for comparison with the second dictionary.

Thanks for the help,
Rachel

+4
3

. , . 0L 0 . ShouldBeEquivalentTo . , , , .

+1

[ , ]

:

FluentAssertions?

FluentAssertions "ShouldBeEquivelentOf" - , , "" (byRef)

FluentAssertions - "Should(). Be (...) objectList.GetType().Should().Be(typeof(List<Classes.SomeListObject>));

0

The 5.0.0-beta.1library version now supports Should().BeEquivalentTo()without type conversion from the box.

https://github.com/fluentassertions/fluentassertions/releases/tag/5.0.0-beta.1 https://github.com/fluentassertions/fluentassertions/pull/616

You can select the type of conversion with WithAutoConversion().

0
source

Source: https://habr.com/ru/post/1544110/


All Articles