如何创建 ObjectSet 的实例?

发布于 2024-11-18 08:29:03 字数 600 浏览 4 评论 0原文

我正在项目中进行 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

岁吢 2024-11-25 08:29:03

你在测试什么?除非您正在单元测试 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 of IObjectSet instead. There is no way to get instance of ObjectSet without the context.

久伴你 2024-11-25 08:29:03
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>();
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>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文