如何创建 ObjectSet 的实例?
我正在项目中进行 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(...));
I am making DAO unit tests in my project and I have a problem using the ObjectSet
class. I have to create a new ObjectSet
but in order to do this, I must not connect to the DB. So I can not use the BusinessModelContainer
's CreateObjectSet()
method. Is there is a way to create the ObjectSet
without it?
The unit test code is like this:
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(...));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你在测试什么?除非您正在单元测试 EF 代码,否则您的单元测试中不需要真正的
ObjectSet
实例。请改用IObjectSet
的模拟。如果没有上下文,就无法获取ObjectSet
的实例。What are you testing? You should not need instance of real
ObjectSet
in your unit test unless you are unit testing EF code. Use mock ofIObjectSet
instead. There is no way to get instance ofObjectSet
without the context.