使用Mock IDbSet对实体框架进行单元测试

The*_*son 10 c# asp.net unit-testing mocking repository

我以前从来没有真正做过单元测试,而且我在第一次测试时偶然发现并绊倒了.问题是_repository.Golfers.Count();始终表明它DbSet是空的.

我的测试很简单,我只想添加一个新的高尔夫球手

[TestClass]
public class GolferUnitTest //: GolferTestBase
{
    public MockGolfEntities _repository;

    [TestMethod]
    public void ShouldAddNewGolferToRepository()
    {
        _repository = new MockGolfEntities();

        _repository.Golfers = new InMemoryDbSet<Golfer>(CreateFakeGolfers());

        int count = _repository.Golfers.Count();
        _repository.Golfers.Add(_newGolfer);

        Assert.IsTrue(_repository.Golfers.Count() == count + 1);
    }

    private Golfer _newGolfer = new Golfer()
    {
        Index = 8,
        Guid = System.Guid.NewGuid(),
        FirstName = "Jonas",
        LastName = "Persson"
    };
    public static IEnumerable<Golfer> CreateFakeGolfers()
    {
        yield return new Golfer()
        {
            Index = 1,
            FirstName = "Bill",
            LastName = "Clinton",
            Guid = System.Guid.NewGuid()
        };
        yield return new Golfer()
        {
            Index = 2,
            FirstName = "Lee",
            LastName = "Westwood",
            Guid = System.Guid.NewGuid()
        };
        yield return new Golfer()
        {
            Index = 3,
            FirstName = "Justin",
            LastName = "Rose",
            Guid = System.Guid.NewGuid()
        };
    }
Run Code Online (Sandbox Code Playgroud)

我使用Entity Framework和代码优先构建了一个数据模型.我已经为IDbSet嘲笑了一个派生类,以便测试我的上下文(对网上的人来说,我不记得确切)

public class InMemoryDbSet<T> : IDbSet<T> where T : class
{
    readonly HashSet<T> _set;
    readonly IQueryable<T> _queryableSet;

    public InMemoryDbSet() : this(Enumerable.Empty<T>()) { }

    public InMemoryDbSet(IEnumerable<T> entities)
    {
        _set = new HashSet<T>();

        foreach (var entity in entities)
        {
            _set.Add(entity);
        }

        _queryableSet = _set.AsQueryable();
    }

    public T Add(T entity)
    {
        _set.Add(entity);
        return entity;

    }

    public int Count(T entity)
    {
        return _set.Count();
    }

    // bunch of other methods that I don't want to burden you with
}
Run Code Online (Sandbox Code Playgroud)

当我调试并逐步执行代码时,我可以看到我实例化_repository并用三个伪造的高尔夫球手填充它,但是当我退出添加功能时,_respoistory.Golfers它再次为空.当我添加一个新的高尔夫球手时,_set.Add(entity)跑步和高尔夫球手被添加,但再次_respoistory.Golfers是空的.我在这里错过了什么?

更新

我很抱歉是个白痴,但我没有实现set我的MockGolfEntities背景.我没有的原因是我之前尝试但无法弄清楚如何,继续前进并忘记它.那么,我该怎么设置IDbSet?这是我尝试过的,但它给了我一个Stack Overflow错误.我觉得自己像个白痴,但我无法弄清楚如何编写set函数.

public class MockGolfEntities : DbContext, IContext
{
    public MockGolfEntities() {}

    public IDbSet<Golfer> Golfers { 
        get {
            return new InMemoryDbSet<Golfer>();
        }
        set {
            this.Golfers = this.Set<Golfer>();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*eta 5

您不需要实现get/set,以下代码应该足以为您生成上下文.

public class MockGolfEntities : DbContext, IContext
{
    public MockGolfEntities() {}

    public IDbSet<Golfer> Golfers { get; set;}

}
Run Code Online (Sandbox Code Playgroud)

我已经实现了原始帖子中的代码,一切似乎都适合我 - 你在哪里获得了InMemoryDbSet的源代码?我正在使用NuGet包1.3,也许你应该尝试那个版本?