How can I create an instance of an ObjectSet?
Asked Answered
M

2

7

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(...));  
Mew answered 2/7, 2011 at 20:50 Comment(0)
K
12

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.

Kamikamikaze answered 2/7, 2011 at 21:7 Comment(0)
I
3
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>();
Idler answered 3/4, 2012 at 3:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.