c# – 使用Mock IDbSet进行单元测试实体框架

前端之家收集整理的这篇文章主要介绍了c# – 使用Mock IDbSet进行单元测试实体框架前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我从来没有真正做过单元测试,而且我偶然发现了我的第一个测试.问题是_repository.Golfers.Count();始终表示DbSet为空.

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

  1. [TestClass]
  2. public class GolferUnitTest //: GolferTestBase
  3. {
  4. public MockGolfEntities _repository;
  5.  
  6. [TestMethod]
  7. public void ShouldAddNewGolferToRepository()
  8. {
  9. _repository = new MockGolfEntities();
  10.  
  11. _repository.Golfers = new InMemoryDbSet<Golfer>(CreateFakeGolfers());
  12.  
  13. int count = _repository.Golfers.Count();
  14. _repository.Golfers.Add(_newGolfer);
  15.  
  16. Assert.IsTrue(_repository.Golfers.Count() == count + 1);
  17. }
  18.  
  19. private Golfer _newGolfer = new Golfer()
  20. {
  21. Index = 8,Guid = System.Guid.NewGuid(),FirstName = "Jonas",LastName = "PeRSSon"
  22. };
  23. public static IEnumerable<Golfer> CreateFakeGolfers()
  24. {
  25. yield return new Golfer()
  26. {
  27. Index = 1,FirstName = "Bill",LastName = "Clinton",Guid = System.Guid.NewGuid()
  28. };
  29. yield return new Golfer()
  30. {
  31. Index = 2,FirstName = "Lee",LastName = "Westwood",Guid = System.Guid.NewGuid()
  32. };
  33. yield return new Golfer()
  34. {
  35. Index = 3,FirstName = "Justin",LastName = "Rose",Guid = System.Guid.NewGuid()
  36. };
  37. }

我已经使用实体框架和代码优先建立了一个数据模型.我为IDbSet嘲笑一个派生类,以便测试我的上下文(对于在线的人来说,我完全不记得)

  1. public class InMemoryDbSet<T> : IDbSet<T> where T : class
  2. {
  3. readonly HashSet<T> _set;
  4. readonly IQueryable<T> _queryableSet;
  5.  
  6. public InMemoryDbSet() : this(Enumerable.Empty<T>()) { }
  7.  
  8. public InMemoryDbSet(IEnumerable<T> entities)
  9. {
  10. _set = new HashSet<T>();
  11.  
  12. foreach (var entity in entities)
  13. {
  14. _set.Add(entity);
  15. }
  16.  
  17. _queryableSet = _set.AsQueryable();
  18. }
  19.  
  20. public T Add(T entity)
  21. {
  22. _set.Add(entity);
  23. return entity;
  24.  
  25. }
  26.  
  27. public int Count(T entity)
  28. {
  29. return _set.Count();
  30. }
  31.  
  32. // bunch of other methods that I don't want to burden you with
  33. }

当我调试并逐步通过代码时,我可以看到我实例化了_repository并填充了三个假的高尔夫球手,但是当我退出添加功能时,_respoistory.Golfers再次为空.当我添加一个新的高尔夫球手,_set.Add(实体)运行,并且添加了高尔夫球手,但是再次_respoistory.Golfers是空的.我在这里缺少什么?

更新

我很抱歉成为一个白痴,但是我没有在我的MockGolfEntities上下文中实现该集.我没有的原因是我以前尝试,但无法弄清楚如何,继续,忘记了.那么,如何设置一个IDbSet?这是我试过的,但它给我一个Stack Overflow错误.我觉得像一个白痴,但是我无法弄清楚如何写入set函数.

  1. public class MockGolfEntities : DbContext,IContext
  2. {
  3. public MockGolfEntities() {}
  4.  
  5. public IDbSet<Golfer> Golfers {
  6. get {
  7. return new InMemoryDbSet<Golfer>();
  8. }
  9. set {
  10. this.Golfers = this.Set<Golfer>();
  11. }
  12. }
  13. }

解决方法

您不需要实现get / set,以下代码应该足以为您生成上下文.
  1. public class MockGolfEntities : DbContext,IContext
  2. {
  3. public MockGolfEntities() {}
  4.  
  5. public IDbSet<Golfer> Golfers { get; set;}
  6.  
  7. }

我已经实现了您在原始帖子中的代码,并且似乎对我来说,所有内容都可以正常工作 – 您在哪里可以获得InMemoryDbSet的源代码?我在使用NuGet package 1.3,也许你应该尝试那个版本?

猜你在找的C#相关文章