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.

How to exclude members from BeEquivalentTo that are null

See original GitHub issue

Hi,

I have code like this:

var user = table.CreateInstance<User>();
userDTO.Should().BeEquivalentTo(user, options => options.ExcludingMissingMembers());

user: obraz

example userDTO:

{ "email": "testuser01@kl;kl;.com", "password": "nn91PKBrv99yQc/uNqTM2", "id": 2, "name": "Ervin Howell", "username": "Antonette", "address": { "street": "Victor Plains", "suite": "Suite 879", "city": "Wisokyburgh", "zipcode": "90566-7771", "geo": { "lat": "-43.9509", "lng": "-34.4618" } }, "phone": "010-692-6593 x09125", "website": "anastasia.net", "company": { "name": "Deckow-Crist", "catchPhrase": "Proactive didactic contingency", "bs": "synergize scalable supply-chains" } },

I want assert only fields that are not null in user but get exception:

'Expected member Email to be <null>, but found "testuser01@dfhdfh.com". Expected member Password to be <null>, but found "O5jG6//tFKinn91PKBrv99yQc/uNqTM2". Expected member Company to be <null>, but found

Null values still are checked. How to not check null values?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (8 by maintainers)

github_iconTop GitHub Comments

4reactions
jnyrupcommented, Jun 18, 2019

Here’s an example on how to recursively ignore members from the expectation, when their runtime value is null.

class User
{
    public string Name { get; set; }

    public string Address { get; set; }
}

class UserDTO
{
    public string Name { get; set; }

    public string Address { get; set; }
}

class IgnoreNullMembersInExpectation : IEquivalencyStep
{
    public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) => context.Expectation is null;

    public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config) => true;
}

[TestClass]
public class IgnoreNullMembers
{
    [TestMethod]
    public void User_and_UserDTO_only_differs_by_null_property()
    {
        // Arrange
        var userDTO = new UserDTO
        {
            Name = "foo",
            Address = "bar"
        };

        var user = new User
        {
            Name = "foo",
            Address = null
        };

        // Assert
        userDTO.Should().BeEquivalentTo(user, opt => opt
            .Using(new IgnoreNullMembersInExpectation()));
    }

    [TestMethod]
    public void User_and_UserDTO_differs_by_non_null_property()
    {
        // Arrange
        var userDTO = new UserDTO
        {
            Name = "foo",
            Address = "bar"
        };

        var user = new User
        {
            Name = "baz",
            Address = null
        };

        // Assert
        userDTO.Should().NotBeEquivalentTo(user, opt => opt
            .Using(new IgnoreNullMembersInExpectation()));
    }
}
3reactions
dennisdoomencommented, Jun 18, 2019

To be honest, I think it’s a scary way to compare two types. Just imagine what happens if Username suddenly become null. But if your intention is to compare only certain properties, you can pass in an anonymous type instead of user that only defines the properties that you care about.

Read more comments on GitHub >

github_iconTop Results From Across the Web

FluentAssertions won't exclude missing members on object ...
I'm trying to exclude these extra properties with the ExcludingMissingMembers options and even explicitly by excluding each member by itself ...
Read more >
Object graph comparison
If you want to exclude certain (potentially deeply nested) individual members using the Excluding() method: orderDto.Should().BeEquivalentTo(order, ...
Read more >
Upgrading to version 6.0 - Fluent Assertions
When comparing object graphs with enum members, we have constrained when we consider them to be equivalent. An enum is now only considered...
Read more >
Customizing Comparisons in FluentAssertions with Options
It's far from obvious, what I was trying to achieve: test whether CopySettingsFrom method copies the right subset of properties from one object ......
Read more >
Documentation - TypeScript 3.7
At its core, optional chaining lets us write code where TypeScript can immediately stop running some expressions if we run into a null...
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