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.

AllBeEquivalentTo not working with Generic Lists

See original GitHub issue

Description

Comparing two identical generic lists using AllBeEquivalentTo failing.

Complete minimal example reproducing the issue

     class Dto
    {
        public string Name { get; set; }
        public string Value { get; set; }
    }
        [TestMethod]
        public void TestBug()
        {
            var list1 = new List<Dto>
            {
                new Dto() {Name = "ABC", Value = "123"}
            };

            var list2 = new List<Dto>
            {
                new Dto() {Name = "ABC", Value = "123"}
            };

            list1.Should().AllBeEquivalentTo(list2);

        }

NOTE: This is fails if I do this.

list1.Should().AllBeEquivalentTo(list1);

Expected behavior:

I believe this should pass.

Actual behavior:

Fails with a message that looks like this.

Expected item[0] to be {Tdx.Puf.Conversion.Tests.Dto
   {
      Name = "ABC"
      Value = "123"
   }}, but found 

Tdx.Puf.Conversion.Tests.Dto
{
   Name = "ABC"
   Value = "123"
}.

With configuration:
- Use declared types and members
- Compare enums by value
- Include all non-private properties
- Include all non-private fields
- Match member by name (or throw)
- Try conversion of all members. 
- Try conversion of all members. 
- Be strict about the order of items in byte arrays

Versions

  • Fluent Assertions 5.3.2.0
  • .Net 4.7.1

Additional Information

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jnyrupcommented, Oct 28, 2018

You’re using List<BlockContentItem> and List<InlineContentItem> and insert TextBlocks, and TextContentItems. Per default BeEquivalentTo will use the compile-time types, so it will only compare the properties declared on BlockContentItem and InlineContentItem.

To use the run-time types use RespectingRuntimeTypes().

0reactions
markdemichcommented, Jun 1, 2018

That did the trick. Thanks so much for the quick reply. I’m going to close this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# generic list subtype operations not working
Ignoring the fact that its almost never desirable to inherit from List<T> , you could create a copy constructor:
Read more >
Collections
AllBeEquivalentTo (referenceObject);. In case if you need to perform individual assertions on all elements of a collection, you can assert each element ...
Read more >
CA1002: Do not expose generic lists (code analysis) - .NET
To fix a violation of this rule, change the System. Collections. Generic. List<T> type to one of the generic collections that's designed for ......
Read more >
List<T>.Count Property (System.Collections.Generic)
The following example demonstrates how to check the capacity and count of a List<T> that contains a simple business object, and illustrates using...
Read more >
Generic List Collection in C# with Examples
In this article, I am going to discuss Generic List Collection Class in C# with Examples. List represents a strongly typed list of...
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