簡體   English   中英

模擬實體框架存儲庫

[英]Mocking entity framework repository

我將MEF用於DI,將MOQ用於模擬。

與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;
    }
}

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM