How can I compare null and string.Empty (or "") in fluent assertions?

HardLuck

I have two objects of the same type, the type has a string field, in the first object the value is null, in the second one the value is "", how can I force fluent assesrtions to assume that this is correct?

Assesrtion itself:

callResult.ShouldBeEquivalentTo(centreResponse, 
                                opt => opt.Excluding(r => r.DateOpen));

here the exception is raised, stating the the expected value is null, the real one is "" (or vice versa)

Dennis Doomen

What you can do is to override the behavior for properties of type string like this:

callResult.ShouldBeEquivalentTo(centreResponse, opt => opt
          .Excluding(r => r.DateOpen)
          .Using<string>(ctx => CompareStrings(ctx)).WhenTypeIs<string>());       

public void CompareStrings(IAssertionContext<string> ctx)
{
    var equal = (ctx.Subject ?? string.Empty).Equals(ctx.Expectation ?? string.Empty);

    Execute.Assertion
        .BecauseOf(ctx.Because, ctx.BecauseArgs)
        .ForCondition(equal)
        .FailWith("Expected {context:string} to be {0}{reason}, but found {1}", ctx.Subject, ctx.Expectation);
}    

You can clean this up a bit by encapsulating the CompareStrings method in an implementation of IAssertionRule. See the RelaxingDateTimeAssertionRule in the unit tests of FluentAssertions here.

You can add that custom assertion rule as the default for all assertions on your the type of your callResult variable, but I still have to add something to allow global defaults.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Fluent Assertions compare string to Guid

From Dev

Fluent Assertions compare string to Guid

From Dev

Compare objects which can be null and empty string ("")

From Dev

How to compare nested lists in object graphs using fluent assertions

From Dev

How to compare nested lists in object graphs using fluent assertions

From Java

How can I check if a string is null or empty in PowerShell?

From Java

How can I check for an empty/undefined/null string in JavaScript?

From Dev

Fluent Assertions: Approximately compare a classes properties

From Dev

How can I compare a string using or ||...

From Dev

How can i compare string and character types?

From Dev

How can I tell Dozer to bypass mapping null or empty string values per field using the programming api?

From Dev

How can i use Empty String to terminate?

From Dev

Fluent Assertions: Approximately compare two 2D rectangular arrays

From Dev

Fluent Assertions: Approximately compare two 2D rectangular arrays

From Dev

How to check a list is ordered using Fluent Assertions

From Dev

How I can compare Null entity without throws NullPointerException

From Dev

How to compare a string null laravel

From Dev

How to compare a string null laravel

From Dev

How to compare against fields that are `null` (empty)?

From Dev

How can I set the limit on a Fluent Query?

From Dev

How can I replace empty value to null with SQL?

From Dev

How can I indicate to the user which textBox is empty or null?

From Dev

How can I avoid converting an empty HashMap to null in morphia?

From Dev

How can I get an empty list instead of null?

From Dev

How can I compare text field and password field to String values?

From Dev

How can I compare a richtextbox to a string in WPF/c#

From Dev

How can I compare a string to a "filter" list in linq?

From Dev

How can i compare a string with 3 other strings?

From Dev

How can I compare out.writeObject("") with string?

Related Related

  1. 1

    Fluent Assertions compare string to Guid

  2. 2

    Fluent Assertions compare string to Guid

  3. 3

    Compare objects which can be null and empty string ("")

  4. 4

    How to compare nested lists in object graphs using fluent assertions

  5. 5

    How to compare nested lists in object graphs using fluent assertions

  6. 6

    How can I check if a string is null or empty in PowerShell?

  7. 7

    How can I check for an empty/undefined/null string in JavaScript?

  8. 8

    Fluent Assertions: Approximately compare a classes properties

  9. 9

    How can I compare a string using or ||...

  10. 10

    How can i compare string and character types?

  11. 11

    How can I tell Dozer to bypass mapping null or empty string values per field using the programming api?

  12. 12

    How can i use Empty String to terminate?

  13. 13

    Fluent Assertions: Approximately compare two 2D rectangular arrays

  14. 14

    Fluent Assertions: Approximately compare two 2D rectangular arrays

  15. 15

    How to check a list is ordered using Fluent Assertions

  16. 16

    How I can compare Null entity without throws NullPointerException

  17. 17

    How to compare a string null laravel

  18. 18

    How to compare a string null laravel

  19. 19

    How to compare against fields that are `null` (empty)?

  20. 20

    How can I set the limit on a Fluent Query?

  21. 21

    How can I replace empty value to null with SQL?

  22. 22

    How can I indicate to the user which textBox is empty or null?

  23. 23

    How can I avoid converting an empty HashMap to null in morphia?

  24. 24

    How can I get an empty list instead of null?

  25. 25

    How can I compare text field and password field to String values?

  26. 26

    How can I compare a richtextbox to a string in WPF/c#

  27. 27

    How can I compare a string to a "filter" list in linq?

  28. 28

    How can i compare a string with 3 other strings?

  29. 29

    How can I compare out.writeObject("") with string?

HotTag

Archive