【问题标题】:Converting an IQueryable<T> to a DbSet<T>将 IQueryable<T> 转换为 DbSet<T>
【发布时间】:2013-09-09 04:25:48
【问题描述】:

我不确定这是否可行,但我正在尝试对使用 DbSet 的存储库进行单元测试。我认为最简单的解决方案就是制作一个 Enumerable,然后用它替换 DbSet,这是我的尝试。

我正在使用 C#、EntityFramework、XUnit 和 Moq

[Fact]
public void SomeTest() 
{
    //arrange
    var mockContext = new Mock<MyDbContext>();
    var mockData = new List<Person>
        {
            new Person { Name = "Jim", Age = 47 }
        };
    mockContext.Setup(db => db.Persons).Returns((DbSet<Person>)mockData.AsQueryable());

    var repo = PersonRepository(mockContext.Object); 

    //act
    var result = repo.GetByFirstName("Jim");

    //assert
    //do some assertion
}

引发的错误是它无法在 mockContext.Returns 语句上将类型 EnumerableQuery 转换为 DbSet。

这与界面的外观类似。

PersonRepository.cs

public class PersonRepository: EFRepository<Person>, IPersonRepository
{
    public PersonRepository(DbContext dbContext) : base(dbContext)
    {
    }

    public IQueryable<Link> GetByFirstName(string name)
    {
        return DbSet.Where(p => p.FirstName == name);
    }
}

IPersonRepository.cs

public interface IPersonRepository: IRepository<Person>
{
    IQueryable<Person> GetByFirstName(string name);
}

IRepository.cs

public interface IRepository<T> where T : class 
{
    IQueryable<T> GetAll();
    T GetById(int id);
    void Add(T entity);
    void Delete(T entity);
    void Delete(int id);
    void Update(T entity);
}

EFRepository.cs

public class EFRepository<T> : IRepository<T> where T : class
{
    protected DbContext DbContext { get; set; }
    public IDbSet<T> DbSet { get; set; }

    public EFRepository(DbContext dbContext)
    {
        if (dbContext == null)
            throw new ArgumentNullException("dbContext");

        DbContext = dbContext;
        DbSet = DbContext.Set<T>();
    }
    ...
}

【问题讨论】:

  • 如果您查看 DbSet 的定义,您会发现它继承自 IEnumerable,因此您可能需要在接口处进行抽象(IEnumerable)水平。
  • 它也继承自 IQueryable 和 IEnumerable。

标签: c# unit-testing iqueryable dbset


【解决方案1】:

您可以做的是将 Linq 表达式从您的存储库移动到业​​务逻辑中,并改为模拟存储库。

public interface IPersonRepository : IRepository<Person>
{
    IQueryable<Person> GetAll { get; }
}

public class PersonRepository : EFRepository<Person>, IPersonRepository
{
    // ...

    public IQueryable<Person> GetAll
    {
        get { return DbSet; }
    }
}

public class SomeBusinessLogicClass
{
    private readonly IPersonRepository _people;

    public SomeBusinessLogicClass(IPersonRepository people)
    {
        _people = people;
    }

    public IEnumerable<Person> GetByFirstName(string name)
    {
        return _people.GetAll.Where(p => p.FirstName == name);
    }
}

现在重写您的测试以验证业务逻辑。

[Fact]
public void SomeTest() 
{
    //arrange
    var mockRepository = new Mock<IPersonRepository>();
    var mockData = new List<Person>
        {
            new Person { Name = "Jim", Age = 47 }
        };
    mockRepository.Setup(x => x.GetAll).Returns(mockData);

    var bl = new SomeBusinessLogicClass(mockRepository.Object); 

    //act
    var result = bl.GetByFirstName("Jim");

    //assert
    //do some assertion
}

【讨论】:

  • 我用更多代码更新了这个问题。我不太明白我将如何去做你所说的。基本上是说将我的存储库的数据源公开为 IQueryable 类型的属性?在我的正常实现中,将其连接到 DbSet,并且为了测试,只需放入我自己的模拟数据源?这样做有什么坏处吗?
  • 我已经根据您添加的代码完全重写了答案。
猜你喜欢
  • 2014-01-30
  • 1970-01-01
  • 1970-01-01
  • 2010-11-18
  • 2012-01-22
  • 1970-01-01
  • 2010-09-11
  • 1970-01-01
  • 2012-11-15
相关资源
最近更新 更多