fluentassertions: BeEquivalentTo on non-generic dictionary gives confusing error
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
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Comments: 17 (10 by maintainers)
We spent 17 months writing v7 🙃
We try to follow semantic versioning such that all minor and patch updates should just be bumping the nuget version. So upgrading a major version will often include some work on the consumer side and even here we carefully consider if a change is justified to minimize the hassle of upgrading.
We both have a changelog for v6 (as well as every other release) and made a migration guide to v6. While the migration guide doesn’t specifically mentions this case (as we do consider it a regression), the release notes mentions both dropping some support for non-generic collections and reworking equivalency for collection types.
We designed this library for extensibility, so until we release v7 with a proper fix for this bug, you can use the extension code below to use utilize method overload resolution to match
Dictionary<,>
toIDictionary
.