0

Summary: While writing unit tests using Fluent Assertions, I've run into a couple of issues which I've described in detail below. Please let me know if you need any additional info from me, but I'm not sure if this is indeed expected behavior or I've misunderstood something.

Issue 1 is an object reference not set to an instance exception that I'm observing. Issue 2 is a behavior that seems unexpected compared to the way the ExcludeMissingProperties is defined.

For my questions below, I'm using the following sample classes that'll help demonstrate the issue I'm talking about.

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

        public int Id { get; set; }
    }

internal class BTo
    {
        internal int Id { get; set; }

    }

public class C
    {
        public int ID { get; set; }
        public B B { get; set; }
    }

Issue 1: Object reference not set to an instance exception when the expectation doesn't have a complex type property set.

a. If an expected object (expectation) does not set a complex type property, and the actual (subject) does, an object reference is not set to an instance error is thrown. This may be fine for simple objects, but in real test scenarios where DTOs are heavily nested, it becomes difficult to find what was causing the exception.

b. Related to this exception, if we can throw the name of the property that was causing an exception it would be very useful. I understand this is an unhandled scenario.

Sample test:

var expected = new C
                {
                    ID = 1,
                };
 var actual = new C
                {
                    ID = 1,
                    B = new B
                        {
                            Id = 1,
                            Name = "name"
                        }
                };
actual.ShouldBeEquivalentTo(expected, expr => expr.ExcludingMissingProperties());

Issue 2: The definition of ExcludingMissingProperties I had to write a lot of code to work around the default behavior.

For a lot of my test cases, I don't want to set the expectation properties; eg: database insert scenarios where I'd like to check that a primary key was generated (so the value should not be default is a sufficient case), but I don't care what the value is. Similarly, there are other noisy properties that are not relevant to my test cases.

So this fails:

var expected = new B { Name = "somevalue"};
var actual = new B { Id = 1, Name = "somevalue" };

actual.ShouldBeEquivalentTo(expected, x => x.ExcludingMissingProperties());

This passes:

var expected = new BTo { Id = 1};
var actual = new B { Id = 1, Name = "somevalue" };

actual.ShouldBeEquivalentTo(expected, e => e.ExcludingMissingProperties());
John Saunders
  • 160,644
  • 26
  • 247
  • 397
m27
  • 79
  • 1
  • 9

1 Answers1

0

Issue 1 has been resolved in the upcoming v3.0 (now in alpha) and was caused by a null object.
Issue 2 can be resolved by using the Excluding method using a property path expression that points to the property to exclude. If you can't use the expression, you can also use an overload of Excluding takes a Func<ISubjectInfo, bool>.

Dennis Doomen
  • 8,368
  • 1
  • 32
  • 44