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.

Detailed message for graph comparison of C# 9 records

See original GitHub issue

Hi! When used with regular classes, comparing two different objects yields a message detailing what exactly is different, like in my case:

Expected member WorkingHoursJson to be <null>, but found 
"[{"dayOfWeek": "friday", "latestTime": 120, "earliestTime": 60}]"

When I make that class a record, those details are lost, and both objects are just dumped into message: Expected createdBusiness to be BusinessDto { Id = 1, Name = Halvorson - Orn, CreatedAt = 12.09.2020 13:47:54, UpdatedAt = 12.09.2020 13:47:54, EnableA... …and so on. Is there a way to get detailed messages for records? Thanks!

EDIT: found ComparingByMembers option, this solves the problem. But I think it would be better to compare records by members by default.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:7
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

7reactions
Cherepoccommented, Jan 15, 2021

Judging by the project I work in, records are used differently then structs. They are either used as local helpers (and are not compared in tests), or as Dtos, and that’s one of the reasons why they were created, as far as I know. We just replace old dto classes with records now, because of non-destructable mutations. Many of those dto classes are serialized to json and have collections, and are checked in tests for equality. Structs are used in our project primarily to represent some special for of data - DateTime, coordinates, etc. It makes sense to compare them by values, because they might have a very special way to compare the data and we can’t know that just by looking at the properties. But that is not the case with records, they are quite the same as tuples in that regard. They are structs too, yet they are not regarded as such for assertions. What makes records different? Anyway, that is just a perspective of a person with experience of using records in just one project. I don’t pretend to be right here. Thanks!

6reactions
InspiringCodecommented, Feb 25, 2021

Meanwhile you can use this workaround to use ComparingByMembers for ALL record types:

static TestBase()
{
    AssertionOptions.EquivalencySteps.Insert<RecordEquivalencyStep>();
}

private class RecordEquivalencyStep : IEquivalencyStep
{
    private readonly StructuralEqualityEquivalencyStep _structuralStep = new StructuralEqualityEquivalencyStep();

    public bool CanHandle(IEquivalencyValidationContext context, IEquivalencyAssertionOptions config) => 
        context.Expectation != null &&
        IsRecord(context.RuntimeType) && 
        _structuralStep.CanHandle(context, config);

    public bool Handle(IEquivalencyValidationContext context, IEquivalencyValidator parent, IEquivalencyAssertionOptions config)
        => _structuralStep.Handle(context, parent, config);

    private static bool IsRecord(Type type) =>
        type.GetMethod("<Clone>$") != null &&
        type.GetTypeInfo()
            .DeclaredProperties
            .FirstOrDefault(p => p.Name == "EqualityContract")?
            .GetMethod?
            .GetCustomAttribute(typeof(CompilerGeneratedAttribute)) != null;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Records in C# 9 - InfoQ
Introducing records​​ C# 9 introduces records, a new reference type for encapsulating data developers can use instead of classes and structs. ...
Read more >
Do C#9 Record Types Take Longer to Perform Equality ...
Records have a protected EqualityContract property of type Type that it compares before comparing the properties. That involves a call to Type.
Read more >
16 Best Types of Charts and Graphs for Data Visualization ...
4. Stacked Bar Chart. Use this chart to compare many different items and show the composition of each item you're comparing.
Read more >
See 20 Different Types Of Graphs And Charts With Examples
See here 20 types of graphs with examples of when to use them! ... When comparing information it is fundamental to pick a...
Read more >
Valid values for graph elements and attributes
Valid values for graph elements and attributes. Updated on Jul 31, 2019 | 4 minutes to read. These are the valid values for...
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