模拟实体框架存储库

时间:2014-12-10 09:36:46

标签: c# .net entity-framework mocking moq

我使用MEF进行DI和MO​​Q进行模拟。

使用Get()的相同单元测试完全正常但Get(2)绝对没有。 MEF正确初始化并且MOQ也是如此。我一直收到null。它是完全相同的代码,除了我有一个参数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;
    }
}

1 个答案:

答案 0 :(得分:1)

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