Search code examples
c#nunitintegration-testingextension-methodsfluent-assertions

Calling Excluding method in extension method for FluentAssertions.Primitives.ObjectAssertions does not work, but calling it normally works


I have started using FluentAssertions library in my integration tests for the REST endpoints. The problem is I have to compare two entities, but excluding their _id properties. This property is being inherited from my IEntity interface.

public interface IEntity
{
    [BsonId]
    ObjectId _id { get; set; }
}

So for example Log class looks like this

[DataContract]
public class Log : IEntity
{
    [BsonId]
    public ObjectId _id { get; set; }

    public string Message { get; set; }
}

In the test I am comparing them like this and it works

retrieved.Should()
         .BeEquivalentTo(expected, options => options.Excluding(member => member._id));

But when I extract this functionality to extension method for reuse purposes, it does not work. It does not ignore the _id member.

public static class ObjectAssertionExtensions
{
    public static void BeEquivalentToExcludingId<TExpectation>(this ObjectAssertions objectAssertion, TExpectation expectation) where TExpectation : IEntity
    {
        objectAssertion.BeEquivalentTo(expectation, options => options.Excluding(member => member._id));
    }
}

When I change the generic extension method to a specific type Log, then it works like it should. I have prepared minimal project with example here. Is there a way how to get this working please and why does it not work properly? I will try to check the code of FluentAssertions in github repository. Thanks.


Solution

  • The problem is that Fluent Assertions fails to correlate _id of the generic type T to _id of the concrete type Log.

    A similar issue was reported in #1077 and resolved with #1087. As of writing we haven't released new version containing this fix.

    Edit 2019-08-10:

    Fluent Assertions 5.8.0 has been released with the fix.