title: Entity Framework 6 Unit Testing (NSubstitute) - How to test that an object with children does not return children when children are not requested
body: <p>I have a method to get an Employer using an Entity Framework context (Lazy Loading is disabled). Sometimes I want the Employees included, sometimes I don't so I have the following code in my data access class:</p>
  
  <pre><code>public Employer GetEmployer(int employerId, bool includeRelationships)
  {
      Employer employer;
  
      if (includeRelationships)
      {
          employer = (from e in this.context.Employers.Include(e =&gt; e.Employees)
                      where e.EmployerId == employerId
                      select e).SingleOrDefault();
      }
      else
      {
          employer = this.context.Employers.SingleOrDefault(e =&gt; e.EmployerId == employerId);
      }
  
      return employer;
  }
  </code></pre>
  
  <p>From several questions about how to use NSubstitute to substitute EF context returns I have this extension method in my test project to hook up the DbSet calls for substitution (specifically <a href="https://stackoverflow.com/questions/21069986/nsubstitute-dbset-iqueryablet">NSubstitute DbSet / IQueryable&lt;T&gt;</a>):</p>
  
  <pre><code>public static IDbSet&lt;T&gt; Initialise&lt;T&gt;(this IDbSet&lt;T&gt; dbSet, IQueryable&lt;T&gt; data) where T : class
  {
      dbSet.Provider.Returns(data.Provider);
      dbSet.Expression.Returns(data.Expression);
      dbSet.ElementType.Returns(data.ElementType);
      dbSet.GetEnumerator().Returns(data.GetEnumerator());
      return dbSet;
  }
  </code></pre>
  
  <p>This is then used to initialise a substitute set of Employers in the test class:</p>
  
  <pre><code>[TestInitialize]
  public void TestInitialise()
  {
      this.context = Substitute.For&lt;EmployerContext&gt;();
  
      this.dao = new EmployerDao(this.context);
  
      var employers = new List&lt;Employer&gt;();
  
      var employerWithoutEmployee = new Employer { EmployerId = 1 };
  
      employers.Add(employerWithoutEmployee);
  
      var employerWithEmployee = new Employer { EmployerId = 2 };
  
      var employee = new Employee { EmployeeId = 1, EmployerId = 2, Employer = employerWithEmployee };
  
      employerWithEmployee.Employees.Add(employee);
  
      employers.Add(employerWithEmployee);
  
      this.substituteEmployers = Substitute.For&lt;IDbSet&lt;Employer&gt;&gt;().Initialise(employers.AsQueryable());
  
      this.context.Employers.Returns(this.substituteEmployers);
  }
  </code></pre>
  
  <p>So, I now have a test that looks like this:</p>
  
  <pre><code>[TestMethod]
  public void ReturnsEmployerWithNullEmployeeWhenIncludeIsFalse()
  {
      // Assemble
      var expectedEmployer = this.substituteEmployers.First(e =&gt; e.Employees.Any();
  
      var employerId = expectedEmployer.EmployerId;
  
      // Act
      var actualEmployer = this.dao.GetEmployer(employerId, false);
  
      var actualEmployee = actualEmployer.Employees.FirstOrDefault();
  
      // Assert
      Assert.AreSame(expectedEmployer, actualEmployer);
      Assert.IsNotNull(actualEmployer);
      Assert.IsNull(actualEmployee);
      this.context.Employers.ReceivedWithAnyArgs();
  }
  </code></pre>
  
  <p>This test is failing on <code>Assert.IsNull(actualEmployee);</code></p>
  
  <p>In real usage, GetEmployer will return an Employer with no Employee children.</p>
  
  <p>However, because I am substituting an Employer with Employee (because this is what I am testing!) the method is returning the substitute which has an Employee.</p>
  
  <p>How can I test this?</p>
  
  <p>Or, am I testing incorrectly? </p>
  
  <p>Should I instead use the Employer which doesn't have an Employee, because that is what the context would return?</p>
  
  <p>But then doesn't that make the test pointless!?!</p>
  
  <p>I'm thinking myself in circles here...</p>