单元测试:如何测试通用存储库

时间:2018-08-23 12:24:09

标签: c# unit-testing generics moq mstest

我正在尝试测试如下所示的通用存储库

public class GenericRepository<T> : IGenericRepository<T> where T : BaseEntity
{
    protected readonly DbContext DbContext;
    public GenericRepository(DbContext dbContext)
    {
        DbContext = dbContext;
    }

    public string Create(T item)
    {
        if (string.IsNullOrEmpty(item.Id))
        {
            item.Id = Guid.NewGuid().ToString("N");
        }
        item.CreatedAt = DateTime.UtcNow;
        DbContext.Entry(item).State = EntityState.Added;
        DbContext.SaveChanges();
        return item.Id;
        /*using (var dbContext = new MyDbContext())
        {
            item.Id = Guid.NewGuid().ToString("N");
            item.CreatedAt = DateTime.UtcNow;
            dbContext.Entry(item).State = EntityState.Added;
            dbContext.SaveChanges();
            return item.Id;
        }*/
    }
    public T GetById(string id)
    {
        return GetFirst(x => x.Id == id);
    }      

    public T GetFirst(Expression<Func<T, bool>> @where, params Expression<Func<T, object>>[] nav)
    {
        return GetFiltered(nav).FirstOrDefault(where);
        /*using (var context = new MyDbContext())
        {
            return GetFiltered(context, nav).FirstOrDefault(where);
        }*/
    }


    private IQueryable<T> GetFiltered(params Expression<Func<T, object>>[] nav)
    {
        IQueryable<T> q = DbContext.Set<T>();
        return nav.Aggregate(q, (current, n) => current.Include(n));
    }

}

基于microft's testing fundamental site,我尝试编写一些测试用例。

下面是单元测试代码

[TestClass]
public class GenericRepositoryTest
{
    private Foo _foo;
    private IQueryable<Foo> _fooList;
    private Mock<DbSet<Foo>> _mockSet;  

    [TestInitialize]
    public void Setup()
    {
        _foo = new Foo
        {            
            EmailId = "foo@bar.com",
            FirstName = "foo",
            LastName = "bar",               
            ProfileId = 27,
            IsDeleted = false,
        };  

        _fooList = new List<Foo>
        {
            new Foo{EmailId = "one@bar.com", FirstName = "one", LastName = "bar", ProfileId = 28, IsDeleted = false}, 
            new Foo{EmailId = "two@bar.com", FirstName = "two", LastName = "bar", ProfileId = 29, IsDeleted = false},    
        }.AsQueryable();
        _mockSet = new Mock<DbSet<Foo>>();
        _mockSet.As<IQueryable<Foo>>().Setup(m => m.Provider).Returns(_fooList.Provider);
        _mockSet.As<IQueryable<Foo>>().Setup(m => m.Expression).Returns(_fooList.Expression);
        _mockSet.As<IQueryable<Foo>>().Setup(m => m.ElementType).Returns(_fooList.ElementType);
        _mockSet.As<IQueryable<Foo>>().Setup(m=>m.GetEnumerator()).Returns(_fooList.GetEnumerator());
    }

    [TestMethod]
    public void Create_GivenEntity_ReturnsGuidId()
    {
        //Arrange
        var guidId = Guid.NewGuid().ToString("N");
        var dbContext = new Mock<MyDbContext>().Object;

        IGenericRepository<Foo> genericRepository = new Mock<GenericRepository<Foo>>(dbContext).Object;

        //Act
        _waitingQueue.Id = guidId;
        var actualId = genericRepository.Create(_foo);

        //Assert
        Assert.IsNotNull(actualId);
        Assert.AreEqual(actualId, guidId);

    }

    [TestMethod]
    public void GetById_GivenEntityId_ReturnsEntity()
    {
        //Arrange
        var id = Guid.NewGuid().ToString("N");
        _foo.Id = id;

        var mockContext = new Mock<MyDbContext>();
        mockContext.Setup(c => c.Foo).Returns(_mockSet.Object);
        IGenericRepository<Foo> genericRepository = new Mock<GenericRepository<Foo>>(mockContext.Object).Object;

        //Act
        var fooId = genericRepository.Create(_foo);
        var fooObject = genericRepository.GetById(id);

        //Assert
        fooObject.PropertiesShouldEqual(_foo);
    }
}

这里是DbContext

public class MyDbContext : DbContext
{
    public MyDbContext() : base("fakeConnectionString")
    {
    }

    public virtual DbSet<Foo> WaitingQueues { get; set; }
}

我是单元测试的新手,我不知道我采用的方法是否正确。

当前,第一个测试Create_GivenEntity_ReturnsGuidId通过了,但是第二个测试GetById_GivenEntityId_ReturnsEntity失败了。

我得到的错误是有关GenericRepository的GetFirst(Expression<Func<T, bool>> @where, params Expression<Func<T, object>>[] nav)方法的

这是因为GetFiltered(params Expression<Func<T, object>>[] nav)调用GetFirst时返回空值。

是因为此时T对于GenericRepository<T>是未知的吗?

我得到的错误是

System.ArgumentNullException
HResult=0x80004003
Message=Value cannot be null.
Parameter name: source

调试时,我可以看到GetById(string id)方法已将正确的ID传递给其中

谁能建议我该怎么办?以及我应该采取什么方法?

1 个答案:

答案 0 :(得分:0)

您没有设置DbContext.Set(),因此,由于Mock默认情况下处于“宽松行为”状态,因此应返回null。

集合的类型不正确。

// should be Mock<DbSet<Foo>>
private Mock<DbSet<WaitingQueue>> _mockSet;
// what it MyDbContext ? should not it be DbContext ? has it is in 
// GenericRepository
var mockContext = new Mock<MyDbContext>();
// what is the point of this line ?
mockContext.Setup(c => c.WaitingQueues).Returns(_mockSet.Object);
// how to setup
mockContext.Setup(c => c.Set<Foo>()).Returns(_mockSet.Object);

nav始终为空,因为您仅使用谓词调用GetFirst。

public T GetById(string id)
{
     // nav is null
     return GetFirst(x => x.Id == id);
}      

此外,如果要测试类GenericRepository,请不要在测试中模拟它,否则,对其进行单元测试的意义何在?

您要测试的是背后的逻辑,并测试它是否可以处理所有输入。

例如,对于GetById方法,请测试id为null,id为空,id不指向现有实体和成功测试(找到实体)的时间。