title: During test in XUnit EF Core retrieves all possible navigation properties
body: <p>The following code gives different results during testing in XUnit and working normally (here - controller method)</p>
  
  <pre><code>var actual = await _itemService.GetSingleBySpecAsync(
              new ItemWithRelatedFieldsSpecification(2));
  </code></pre>
  
  <p>I am unable to test my methods normally because test behaviour has nothing to do with real results.</p>
  
  <p>The correct result in controller method - the fields I specified are in place and no more
  <a href="https://i.stack.imgur.com/Bys6n.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Bys6n.png" alt="controllerResults"></a></p>
  
  <p>but during testing I see all Categories being retrieved recursively till the very root - I do not need this</p>
  
  <p><a href="https://i.stack.imgur.com/oGVRh.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/oGVRh.png" alt="xunitResults"></a></p>
  
  <p><strong>Edit 1</strong></p>
  
  <pre><code>public class Item : BaseEntity
  {
      public string Title { get; set; }
      public int CategoryId { get; set; }
      public int StoreId { get; set; }
      public int BrandId { get; set; }
      public int MeasurementUnitId { get; set; }
      public string Description { get; set; }
  
      public Category Category { get; set; }
      public Store Store { get; set; }
      public Brand Brand { get; set; }
      public MeasurementUnit MeasurementUnit { get; set; }
      public IEnumerable&lt;FileDetails&lt;Item&gt;&gt; Images { get; set; }
      public IEnumerable&lt;ItemVariant&gt; ItemVariants { get; set; }
  }
  public class Category : BaseEntity
  {
      public string Title { get; set; }
      public int ParentCategoryId { get; set; }
      public bool CanHaveItems { get; set; }
      public string Description { get; set; }
  
      public Category ParentCategory { get; set; }
      public List&lt;Category&gt; Subcategories = new List&lt;Category&gt;();
  }
  public class BaseEntity
  {
      public int Id { get; set; }
  }
  public async Task&lt;Item&gt; GetSingleBySpecAsync(ISpecification&lt;Item&gt; spec)
  {
     return await _itemRepository.GetSingleBySpecAsync(spec);
  }
  public async Task&lt;T&gt; GetSingleBySpecAsync(ISpecification&lt;T&gt; spec)
  {
      return (await ListAsync(spec)).FirstOrDefault();
  }
  public async Task&lt;IEnumerable&lt;T&gt;&gt; ListAsync(ISpecification&lt;T&gt; spec)
  {
      // fetch a Queryable that includes all expression-based includes
      var queryableResultWithIncludes = spec.Includes
         .Aggregate(_dbContext.Set&lt;T&gt;().AsQueryable(),
             (current, include) =&gt; current.Include(include));
       // modify the IQueryable to include any string-based include statements
       var secondaryResult = spec.IncludeStrings
          .Aggregate(queryableResultWithIncludes,
              (current, include) =&gt; current.Include(include));
  
      // return the result of the query using the specification's criteria expression
      return await secondaryResult
                      .Where(spec.Criteria)
                          .ToListAsync();
  }
  public class ItemWithRelatedFieldsSpecification : BaseSpecification&lt;Item&gt;
  {
      public ItemWithRelatedFieldsSpecification(int? id)
          : base(i =&gt; (!id.HasValue || i.Id == id))
      {
          AddInclude(i =&gt; i.MeasurementUnit);
          AddInclude(i =&gt; i.Store);
          AddInclude(i =&gt; i.Category);
          AddInclude(i =&gt; i.Brand);
          AddInclude(i =&gt; i.Images);
      }
  }
  public abstract class BaseSpecification&lt;T&gt; : ISpecification&lt;T&gt;
  {
      public BaseSpecification(Expression&lt;Func&lt;T, bool&gt;&gt; criteria)
      {
          Criteria = criteria;
      }
      public Expression&lt;Func&lt;T, bool&gt;&gt; Criteria { get; }
      public List&lt;Expression&lt;Func&lt;T, object&gt;&gt;&gt; Includes { get; } = new List&lt;Expression&lt;Func&lt;T, object&gt;&gt;&gt;();
      public List&lt;string&gt; IncludeStrings { get; } = new List&lt;string&gt;();
  
      protected virtual void AddInclude(Expression&lt;Func&lt;T, object&gt;&gt; includeExpression)
      {
          Includes.Add(includeExpression);
      }
      protected virtual void AddInclude(string includeString)
      {
          IncludeStrings.Add(includeString);
      }
  }
  </code></pre>