如何创建ObjectSet的实例?

Adr*_*ano 7 c# unit-testing entity-framework objectset

我正在我的项目中进行DAO单元测试,我在使用ObjectSet该类时遇到了问题.我必须创建一个新的,ObjectSet但为了做到这一点,我不能连接到数据库.所以我不能使用BusinessModelContainerCreateObjectSet()方法.有没有办法创造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(...));  
Run Code Online (Sandbox Code Playgroud)

Lad*_*nka 12

你在测试什么?ObjectSet除非您是单元测试EF代码,否则您不应该在单元测试中使用real实例.使用mock来IObjectSet代替.没有ObjectSet上下文就无法获取实例.