chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
Welcome to Typemock Community! Here you can ask and receive answers from other community members. If you liked or disliked an answer or thread: react with an up- or downvote.
0 votes
Hello,

I want to fake an ObjectSet<T> from Entity Framework, has anyone done this at all? Specifically, I want to be able to fake the results that come back out of the query...

Thanks.
asked by bmains (13.2k points)

1 Answer

0 votes
Hi, we know of a limited solution for this problem.

Say you have a query like this:

IQueryable<Course> courseQuery =
                    from c in myLearningEntities.Courses
                    where c.CourseId == courseId
                    select c;


where Courses is an ObjectSet<Course>.

So you could fake the myLearnningEntites.Courses with a code that looks kind of like this:

var fakeEntities = Isolate.Fake.Instance<MyLearningEntities>();
Isolate.Swap.NextInstance<MyLearningEntities>().With(fakeEntities);

var listOfCourses = new List<Course>();
var returnedCourse = new Course { CourseName = "NEO", CourseType = "Instructor-Led", IsMandatoryForEB = false };
listOfCourses.Add(returnedCourse);
Isolate.WhenCalled(() => fakeEntities.Courses).WillReturnCollectionValuesOf(listOfCourses.AsQueryable());



However, if you would try to use methods specific to ObjectSet and not to List, for example if you have a query like this:
IQueryable<Course> courseQuery =
                    from c in myLearningEntities.Courses.Include("CourseType")
                    where c.CourseId == courseId
                    select c;


Then the above test code would not work. You could write an exact WhenCalled chain on the whole lamda expression like so:
Isolate.WhenCalled(() => fakeEntities.Courses.Include("CourseType")).WillReturnCollectionValuesOf(listOfCourses.AsQueryable());



Does this apply to your case?
answered by yoel (1.9k points)
...