Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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(...));  
like image 545
Adriano Avatar asked Jul 02 '11 20:07

Adriano


2 Answers

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.

like image 130
Ladislav Mrnka Avatar answered Nov 11 '22 02:11

Ladislav Mrnka


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>();
like image 3
dirtydenver Avatar answered Nov 11 '22 04:11

dirtydenver