NullReferenceException in BeEquivalentTo() usage with "Using" and "When" EquivalencyAssertationOptions
See original GitHub issue“When” usage throws NullReferenceException after “Using” EquivalencyAssertionOptions
Test objects have the same type and it has CreatedDate, UpdatedDate properties.
Test Code:
input.Should().BeEquivalentTo(result, options => options
.Using<DateTime>(context => context.Subject.Should().BeCloseTo(context.Expectation, 1000))
.When(info => info.SelectedMemberPath.EndsWith("Date")));
Actual behavior:
Result1 StackTrace:
at FluentAssertions.Equivalency.AssertionContext`1.CreateFromEquivalencyValidationContext(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\AssertionContext.cs:line 29
at FluentAssertions.Equivalency.AssertionRule`1.AssertEquality(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\AssertionRule.cs:line 57
at FluentAssertions.Equivalency.AssertionRuleEquivalencyStepAdapter.Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\AssertionRuleEquivalencyStepAdapter.cs:line 22
at FluentAssertions.Equivalency.RunAllUserStepsEquivalencyStep.Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\RunAllUserStepsEquivalencyStep.cs:line 21
at FluentAssertions.Equivalency.EquivalencyValidator.RunStepsUntilEquivalencyIsProven(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 119
at FluentAssertions.Equivalency.EquivalencyValidator.AssertEqualityUsing(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 54
at FluentAssertions.Equivalency.StructuralEqualityEquivalencyStep.Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\StructuralEqualityEquivalencyStep.cs:line 46
at FluentAssertions.Equivalency.EquivalencyValidator.RunStepsUntilEquivalencyIsProven(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 119
at FluentAssertions.Equivalency.EquivalencyValidator.AssertEqualityUsing(IEquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 54
at FluentAssertions.Equivalency.EquivalencyValidator.AssertEquality(EquivalencyValidationContext context) in C:\Workspaces\fluentassertions\src\FluentAssertions\Equivalency\EquivalencyValidator.cs:line 37
at FluentAssertions.Primitives.ObjectAssertions.BeEquivalentTo[TExpectation](TExpectation expectation, Func`2 config, String because, Object[] becauseArgs) in C:\Workspaces\fluentassertions\src\FluentAssertions\Primitives\ObjectAssertions.cs:line 123
Result1 Message: System.NullReferenceException : Object reference not set to an instance of an object.
Result1 StandardOutput:
Result1 StandardError:
Versions
FluentAssertions V5.7.0 .NET framework 4.6.1
Additional Information
Test adapted from “Equivalency Comparison Behavior” of https://fluentassertions.com/objectgraphs/
Issue Analytics
- State:
- Created 4 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
FluentAssertions Should().BeEquivalentTo() fails in trivial ...
It seems it tries to treat the Address object as a string (because it overrides ToString() ?). I tried to use the options.ComparingByMembers< ......
Read more >Upgrading to version 6.0 - Fluent Assertions
If you want to compare two enums of different types, you can use ... An enum is now only considered to be equivalent...
Read more >9 Fluent Assertions Tricks to Save Hours of Your Testing ...
Check for exceptions with Fluent Assertions. Now let's try to use Fluent Assertions to check if the exception is thrown: Action act =...
Read more >Just Use FluentAssertions - Tinkerer
BeEquivalentTo is the real killer feature of FluentAssertions for me. It allows you to compare whether or not two object have equivalent fields....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Maybe we should change that property to never return
null
and return an empty string instead.By looking at the provided stack trace and the fact that the property types sounds to be
DateTime?
this is caused by #799.SelectedMemberPath
is not null, it’sSelectedMemberInfo
that can be null.