我正在项目中进行DAO单元测试,但在使用ObjectSet类时遇到问题。我必须创建一个新的ObjectSet,但为了做到这一点,我不能连接到数据库。所以我不能使用BusinessModelContainer的CreateObjectSet()方法。有没有办法在没有它的情况下创建ObjectSet?
单元测试代码如下:
var mock = new Mock<IBusinessModelContainerWrapper>();
ObjectSet<Student> expectedStudent = ???; // how can I get an instance here?
StudentDao studentDao = new StudentDao(mock.Object);
expectedStudent.Add(someObj);
mock.Setup(c => c.Students).Returns(expectedStudent);
Assert.AreEqual(someObj, studentDao.GetByQuery(...)); 发布于 2011-07-03 05:07:54
你在测试什么?你不应该在你的单元测试中需要真正的ObjectSet实例,除非你在单元测试EF代码。改用IObjectSet的模拟。如果没有上下文,就无法获取ObjectSet的实例。
发布于 2012-04-03 11:22:27
DAL.Moles.MDALEntities.Constructor = (a) => { };
DALEntities db = new DALEntities(); //Object Context
System.Data.Objects.Moles.MObjectContext.AllInstances.CreateObjectSet<ObjectSetType>(
(a) => { return new System.Data.Objects.Moles.MObjectSet<ObjectSetType>(); }
);
ObjectSet<ObjectSetType> dbList = db.CreateObjectSet<ObjectSetType>();https://stackoverflow.com/questions/6559536
复制相似问题