简体   繁体   中英

How can I create an instance of an ObjectSet?

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(...));  

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.

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>();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM