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);
}
here I'm not able to understand what I do wrong in this case. for reference I'm using this link: