收集被修改,枚举操作可能不会在Moq中使用EF

时间:2016-11-29 06:02:11

标签: c# entity-framework unit-testing in-memory-database

在进行单元测试时,我无法在dbset中添加新实体后从dbset中收集回来,它会抛出异常“集合被修改

这是我的代码设置

[TestMethod]
[TestCategory("Skill Category")]
public void Create_Skill_Category()
{
    var category = new SkillCategoryModel() { CategoryId = 3, CategoryName = "Category 3" };
    var result = skillManager.SaveSkillCategory(category);
    Assert.IsNotNull(result, "Category can't be null");
    Assert.AreEqual(category.CategoryId, result.CategoryId, "Category id must be equal");
    var categoryList = skillManager.GetCategories(); // here exception thrown
    Assert.IsTrue(categoryList.Count == 3, "Categories List must be contain three category");
}


private ISkill skillManager;
[TestInitialize]
public void Init()
{
    var category = new SkillCategory { CategoryId = 1, CategoryName = "Category 1" };

    var categories = new List<SkillCategory>
    { 
        category,
        new SkillCategory { CategoryId = 2, CategoryName = "Category 2" }
    };
    var categoryMockSet = Utility.GenerateMockEntity(categories);
    categoryMockSet.Setup(x => x.Add(It.IsAny<SkillCategory>())).Callback<SkillCategory>(x => categories.Add(x)).Returns<SkillCategory>(x => x);
    var mock = new Mock<WhoEntities>();
    mock.Setup(q => q.SkillCategories).Returns(categoryMockSet.Object);
    mock.CallBase = true;
    skillManager = new WhoGroup.DML.Managers.SkillManager(mock.Object);
}

在这里,我无法理解在这种情况下我做错了什么。 供参考我正在使用此链接:

Entity Framework 6 and Moq4: Is it possible to have a mocked DbSet retain added data for the duration of its scope?

2 个答案:

答案 0 :(得分:6)

以下声明不正确:

entityMockSet.As<IEnumerable<TEntity>>()
    .Setup(m => m.GetEnumerator()).Returns(query.GetEnumerator());

每次请求都会返回相同的Enumerator,这会导致您只能使用枚举器一次,因为之后没有任何项目。

部分解决问题的方法是每次调用GetEnumerator时都会重置枚举器,因为您只能使用Add - 方法

<强>解决方案: 真正解决问题的方法是在设置GetEnumerator方法时使用Lambda:

entityMockSet.As<IEnumerable<TEntity>>()
    .Setup(m => m.GetEnumerator()).Returns(() => query.GetEnumerator());

这部分非常重要: .Returns( ()=&gt; query.GetEnumerator()); 因为这样,每次发出请求时都会返回一个新的枚举器。

答案 1 :(得分:0)

moq Dbset中发生错误,因为我在DB集中添加新实体后没有更新GetEnumerator的引用。

 public class Utility
    {
        public static Mock<DbSet<TEntity>> GenerateMockEntity<TEntity>(List<TEntity> entityList) where TEntity : class
        {
            var list = new List<TEntity>();
            list.AddRange(entityList);
            var query = list.AsQueryable();
            var entityMockSet = new Mock<DbSet<TEntity>>() { CallBase = true};
            entityMockSet.As<IQueryable<TEntity>>().Setup(m => m.Provider).Returns(query.Provider);
            entityMockSet.As<IQueryable<TEntity>>().Setup(m => m.Expression).Returns(query.Expression);
            entityMockSet.As<IQueryable<TEntity>>().Setup(m => m.ElementType).Returns(query.ElementType);
            entityMockSet.As<IEnumerable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(query.GetEnumerator());
            entityMockSet.Setup(x => x.Add(It.IsAny<TEntity>())).Callback<TEntity>(x => {
                list.Add(x);
                entityMockSet.As<IEnumerable<TEntity>>().Setup(m => m.GetEnumerator()).Returns(list.GetEnumerator());
            }).Returns<TEntity>(x => x);
            return entityMockSet;
        }
    }