本文介绍了收集被修改,枚举操作可能不会在EF的moq中执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这是我的代码设置

While doing unit testing, i am not able to get collection back from dbset after add new entity in dbset, it throw an exception 'Collection was modified'

Here is my code setup

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







我尝试了什么:



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

推荐答案

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;
        }
    }</tentity></ienumerable<tentity></tentity></tentity></ienumerable<tentity></iqueryable<tentity></iqueryable<tentity></iqueryable<tentity></dbset<tentity></tentity></tentity></tentity></dbset<tentity>


这篇关于收集被修改,枚举操作可能不会在EF的moq中执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:20