title: ShouldBeEquivalentTo causing object reference not set exception / definition of ExcludeMissingProperties
body: <p><strong>Summary</strong>: 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. </p>
  
  <p>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.</p>
  
  <p>For my questions below, I'm using the following sample classes that'll help demonstrate the issue I'm talking about. </p>
  
  <pre><code>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; }
      }
  </code></pre>
  
  <p><strong>Issue 1: Object reference not set to an instance exception when the expectation doesn't have a complex type property set.</strong></p>
  
  <p>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. </p>
  
  <p>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.</p>
  
  <p>Sample test:</p>
  
  <pre><code>var expected = new C
                  {
                      ID = 1,
                  };
   var actual = new C
                  {
                      ID = 1,
                      B = new B
                          {
                              Id = 1,
                              Name = "name"
                          }
                  };
  actual.ShouldBeEquivalentTo(expected, expr =&gt; expr.ExcludingMissingProperties());
  </code></pre>
  
  <p><strong>Issue 2: The definition of ExcludingMissingProperties</strong>
  I had to write a lot of code to work around the default behavior.  </p>
  
  <p>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. </p>
  
  <p><em>So this fails:</em></p>
  
  <pre><code>var expected = new B { Name = "somevalue"};
  var actual = new B { Id = 1, Name = "somevalue" };
  
  actual.ShouldBeEquivalentTo(expected, x =&gt; x.ExcludingMissingProperties());
  </code></pre>
  
  <p><em>This passes:</em></p>
  
  <pre><code>var expected = new BTo { Id = 1};
  var actual = new B { Id = 1, Name = "somevalue" };
  
  actual.ShouldBeEquivalentTo(expected, e =&gt; e.ExcludingMissingProperties());
  </code></pre>