Unit testing Entity Framework with Mock IDbSet

2019-01-23 16:55发布

I've never really done unit testing before, and I've stumbled and tripped on my first test. The problem is that the _repository.Golfers.Count(); always indicates that the DbSet is empty.

My test is simple, I'm just trying to add a new golfer

[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()
        };
    }

I've built a data model using Entity Framework and code-first. I've mocked an derived class for IDbSet in order to test my context (curtsy to people online that I can't remember exactly)

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
}

When I debug and step through the code I can see that I instantiate the _repository and fill it with three faked golfers, but when I step out of the add function, the _respoistory.Golfers is empty again. when I add a new golfer, the _set.Add(entity) runs and the golfer is added, but again _respoistory.Golfers is empty. What am I missing here?

Update

I'm sorry for being an idiot, but I hadn't implemented the set on my MockGolfEntities context. The reason I had not is that I tried before but couldn't figure out how, moved on, and forgot about it. So, how do I set an IDbSet? This is what I've tried, but it gives me a Stack Overflow error. I feel like an idiot, but I can't figure out how to write the set function.

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

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

1条回答
乱世女痞
2楼-- · 2019-01-23 17:31

You shouldn't need to implement the get/set, the following code should be enough to generate the context for you.

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

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

}

I've implemeted the code you had in your original post and everything seemed to work well for me - where did you get the source for the InMemoryDbSet? I'm using the NuGet package 1.3, maybe you should try that version?

查看更多
登录 后发表回答