模拟实体框架存储库

本文关键字:存储 框架 实体 模拟 | 更新日期: 2023-09-27 18:33:51

我用MEF做DI,用MOQ做嘲笑。

与 Get()

相同的单元测试工作得很好,但 Get(2) 绝对不能。 MEF 已正确初始化,最小起订量也是如此。我一直收到空。这是完全相同的代码,除了我有一个 Get() 方法的参数,但有一个参数。我在抽象类中使用GetEntity,而不是像工作测试那样使用GetEntities()。

仅供参考,当我点击数据库时完全没有问题。

public class TestClass
{
    [Import]
    IDataRepositoryFactory _DataRepositoryFactory;
    public TestClass()
    {
        ObjectBase.Container.SatisfyImportsOnce(this);
    }
    public TestClass(IDataRepositoryFactory dataRepositoryFactory)
    {
        _DataRepositoryFactory = dataRepositoryFactory;
    }
    public IEnumerable<Customer> GetCustomers()
    {
        ICustomerRepository customerRepository = _DataRepositoryFactory.GetDataRepository<ICustomerRepository>();
        IEnumerable<Customer> customers = customerRepository.Get();
        return customers;
    }
    public Customer GetCustomers(int id)
    {
        ICustomerRepository customerRepository = _DataRepositoryFactory.GetDataRepository<ICustomerRepository>();
        Customer customer =  customerRepository.Get(id);
        return customer;
    }
}
[TestMethod]
public void GetById()
{
    List<Customer> customers = new List<Customer>()
    {
        new Customer() { CustomerId = 1, FirstName = "AAA" },
        new Customer() { CustomerId = 2, FirstName = "BBB" }
    };
    Mock<ICustomerRepository> mockCustomerRepository = new Mock<ICustomerRepository>();
    mockCustomerRepository.Setup(obj => obj.Get()).Returns(customers);
    Mock<IDataRepositoryFactory> mockDataRepository = new Mock<IDataRepositoryFactory>();
    mockDataRepository.Setup(obj => obj.GetDataRepository<ICustomerRepository>()).Returns(mockCustomerRepository.Object);
    DataClassFactory dataClassFactory = new DataClassFactory(mockDataRepository.Object);
    Customer ret = dataClassFactory.GetCustomers(2);
    Assert.IsNotNull(ret);
}
public interface IDataRepositoryFactory
{
    T GetDataRepository<T>() where T : IDataRepository;
}
public interface IDataRepository{}
public interface IDataRepository<T> : IDataRepository
    where T : class, IIdentifiableEntity, new()
{
    IEnumerable<T> Get();
    T Get(int id);
}
public abstract class DataRepositoryBase<T, U> : IDataRepository<T>
    where T : class, IIdentifiableEntity, new()
    where U : DbContext, new()
{
    protected abstract DbSet<T> DbSet(U entityContext);
    protected abstract Expression<Func<T, bool>> IdentifierPredicate(U entityContext, int id);
    T AddEntity(U entityContext, T entity)
    {
        return DbSet(entityContext).Add(entity);
    }
    IEnumerable<T> GetEntities(U entityContext)
    {
        return DbSet(entityContext).ToFullyLoaded();
    }
    T GetEntity(U entityContext, int id)
    {
        return DbSet(entityContext).Where(IdentifierPredicate(entityContext, id)).FirstOrDefault();
    }
    public IEnumerable<T> Get()
    {
        using (U entityContext = new U())
            return (GetEntities(entityContext)).ToArray().ToList();
    }
    public T Get(int id)
    {
        using (U entityContext = new U())
            return GetEntity(entityContext, id);
    }
}

更新

public class DataClassFactory
{
    [Import]
    IDataRepositoryFactory _DataRepositoryFactory;
    public DataClassFactory()
    {
        ObjectBase.Container.SatisfyImportsOnce(this);
    }
    public DataClassFactory(IDataRepositoryFactory dataRepositoryFactory)
    {
        _DataRepositoryFactory = dataRepositoryFactory;
    }
    public IEnumerable<Customer> GetCustomers()
    {
        ICustomerRepository customerRepository = _DataRepositoryFactory.GetDataRepository<ICustomerRepository>();
        IEnumerable<Customer> customers = customerRepository.Get();
        return customers;
    }
    public Customer GetCustomers(int id)
    {
        ICustomerRepository customerRepository = _DataRepositoryFactory.GetDataRepository<ICustomerRepository>();
        Customer customer =  customerRepository.Get(id);
        return customer;
    }
}

模拟实体框架存储库

GetById测试方法中,您使用dataClassFactory.GetCustomers(2),这会进入GetCustomers(int id)重载。该重载正在调用customerRepository.Get(id)它转到您没有模拟的重载 - 这就是为什么它返回 null。

这应该解决它

Mock<ICustomerRepository> mockCustomerRepository = new Mock<ICustomerRepository>();
mockCustomerRepository.Setup(obj => obj.Get()).Returns(customers);
mockCustomerRepository.Setup(obj => obj.Get(It.IsAny<int>())).Returns((int i) => customers.FirstOrDefault(c => c.CustomerId == i)); // This is the new part