在XUnit EF Core中进行测试期间,将检索所有可能的导航属性

时间:2018-06-24 13:12:23

标签: c# entity-framework-core xunit

以下代码在XUnit中进行测试并正常工作时(此处为-控制器方法)给出不同的结果

var actual = await _itemService.GetSingleBySpecAsync(
            new ItemWithRelatedFieldsSpecification(2));

我无法正常测试我的方法,因为测试行为与实际结果无关。

控制器方法中的正确结果-我指定的字段就位了 controllerResults

但是在测试期间,我看到所有类别都以递归方式检索,直到最根本的时候-我不需要这个

xunitResults

编辑1

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<FileDetails<Item>> Images { get; set; }
    public IEnumerable<ItemVariant> 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<Category> Subcategories = new List<Category>();
}
public class BaseEntity
{
    public int Id { get; set; }
}
public async Task<Item> GetSingleBySpecAsync(ISpecification<Item> spec)
{
   return await _itemRepository.GetSingleBySpecAsync(spec);
}
public async Task<T> GetSingleBySpecAsync(ISpecification<T> spec)
{
    return (await ListAsync(spec)).FirstOrDefault();
}
public async Task<IEnumerable<T>> ListAsync(ISpecification<T> spec)
{
    // fetch a Queryable that includes all expression-based includes
    var queryableResultWithIncludes = spec.Includes
       .Aggregate(_dbContext.Set<T>().AsQueryable(),
           (current, include) => current.Include(include));
     // modify the IQueryable to include any string-based include statements
     var secondaryResult = spec.IncludeStrings
        .Aggregate(queryableResultWithIncludes,
            (current, include) => 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<Item>
{
    public ItemWithRelatedFieldsSpecification(int? id)
        : base(i => (!id.HasValue || i.Id == id))
    {
        AddInclude(i => i.MeasurementUnit);
        AddInclude(i => i.Store);
        AddInclude(i => i.Category);
        AddInclude(i => i.Brand);
        AddInclude(i => i.Images);
    }
}
public abstract class BaseSpecification<T> : ISpecification<T>
{
    public BaseSpecification(Expression<Func<T, bool>> criteria)
    {
        Criteria = criteria;
    }
    public Expression<Func<T, bool>> Criteria { get; }
    public List<Expression<Func<T, object>>> Includes { get; } = new List<Expression<Func<T, object>>>();
    public List<string> IncludeStrings { get; } = new List<string>();

    protected virtual void AddInclude(Expression<Func<T, object>> includeExpression)
    {
        Includes.Add(includeExpression);
    }
    protected virtual void AddInclude(string includeString)
    {
        IncludeStrings.Add(includeString);
    }
}

0 个答案:

没有答案