Robbert Raats
Robbert Raats

Reputation: 259

FluentAssertions Compare two IQueryables

I have two Iqueryables which have different values. I want to use FluentAssertions to compare the elements in the Iqueryable for my Unittests.

What I have is the following:

[Fact]
public void TestCompareIQueryables()
{
    // Arrange
    var objects1 = new CustomObject[]
    {
        new CustomObject {
            Code = "Code1",
            Name = "Name1"
        }
    }.AsQueryable();

    var objects2 = new CustomObject []
    {
        new CustomObject {
            Code = "Code2",
            Name = "Name2"
    }
    }.AsQueryable();

    // Assert
    objects1.Should().HaveSameCount(objects2);
    objects1.Should().BeEquivalentTo(objects1);
    objects1.Should().BeEquivalentTo(objects2);
}

What is happening when I run this fact, is that it passes, but I expect the last Should().BeEquivalentTo() to fail.

Am I missing something that make the test not act as expected here? How can I compare each element to eachother in two IQueryables correctly?

Any help would be appreciated!

Upvotes: 0

Views: 210

Answers (1)

Robbert Raats
Robbert Raats

Reputation: 259

The fix is in another call from FluentAssertions:

objects1.Should().BeSameAs(objects2);

This gives the expected error.

Upvotes: 1

Related Questions