question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 4 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
dennisdoomencommented, Jul 10, 2019

Maybe we should change that property to never return null and return an empty string instead.

0reactions
jnyrupcommented, Feb 13, 2021

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’s SelectedMemberInfo that can be null.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found