Skip to content

BeEquivalentTo on non-generic dictionary gives confusing error #2298

@kbilsted

Description

@kbilsted

Description

upgrading from v5 to version 6.12.0 I no longer am able to assert exception data

Reproduction Steps

 [Test]
public void exception()
{
    Func<Task> act = async () =>
    {
        var ex = new Exception("msg");
        ex.Data.Add("id", 22);
        ex.Data.Add("CustomerId", 33);
        throw ex;
    };
    
    act.Should()
        .ThrowAsync<Exception>()
        .WithMessage("msg")
        .Result.Which.Should().BeEquivalentTo(new Dictionary<object,object>()
        {
            {"id",22},
            {"CustomerId",33}
        });
}

Expected behavior

assert

Actual behavior

Expected act to be a dictionary or collection of key-value pairs that is keyed to type System.Object. It implements .

With configuration:
- Use declared types and members
- Compare enums by value
- Compare tuples by their properties
- Compare anonymous types by their properties
- Compare records by their members
- Include non-browsable members
- Match member by name (or throw)
- Be strict about the order of items in byte arrays
- Without automatic conversion.

   at FluentAssertions.Execution.LateBoundTestFramework.Throw(String message)
   at FluentAssertions.Execution.TestFrameworkProvider.Throw(String message)
   at FluentAssertions.Execution.CollectingAssertionStrategy.ThrowIfAny(IDictionary`2 context)
   at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(Comparands comparands, EquivalencyValidationContext context)
   at FluentAssertions.Primitives.ObjectAssertions`2.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs)
   at FluentAssertions.Primitives.ObjectAssertions`2.BeEquivalentTo[TExpectation](TExpectation expectation, String because, Object[] becauseArgs)
   at Alka.PermissionsService.Tests.AgreementRegistrationTests.exception() in C:\source\xxx.Tests\Tests.cs:line 

Regression?

yes

Known Workarounds

you can build this but its ugly and the assert requires to call this

public static Dictionary<string, object> Extract(this Exception e)
{
    var result = new Dictionary<string, object>();
    var ee = e.Data.GetEnumerator();
    while (ee.MoveNext())
    {
        result.Add(ee.Key.ToString(), ee.Value);
    }
    return result;
}

Configuration

No response

Other information

No response

Are you willing to help with a pull-request?

No

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions