I have a method which is taking my Entity Framework Entity and converting it into a DTO object. In this method I have parameters to skip and limit the number of related items to return. With small data sets a simple query like this worked well:
var query = this.AccessLogs
.Skip(skipRelated)
.Take(takeRelated);
With larger data sets, I found that this actually executed SELECT * at my database and caused lots of problems since I have millions of related records in some cases. After asking this question I modified the query to this:
var query = this.AccessLogs
.CreateSourceQuery()
.OrderBy(p => p.ID)
.Skip(skipRelated)
.Take(takeRelated);
Now, while this fixed the performance problems I had during integration tests, this causes every single one of my unit tests to fail, because .CreateSourceQuery()
returns null and then my .OrderBy()
barfs with an ArgumentNullException on parameter name: source.
I have a repository which returns IQueryable<T>
and I have dependency injection setup to unit test it, so I am setting up my "test" data like this. Originally I was just using List<T>
but I found this article which uses an InMemoryObjectSet<T>
for testing. Either way, my call to.CreateSourceQuery()
returns null,even when there is data in the underlying collection.
IObjectSet<Parent> ret = new InMemoryObjectSet<Parent>();
var parent = new Parent();
parent.ID = 1;
parent.Name = "Name 1";
for(int i = 0; i < 5; i++)
{
var ch = new Child();
ch.ID = i;
ch.ParentID = 1;
ch.Property1 = "Name " + i.ToString();
parent .Children.Add(ch);
}
ret.AddObject(parent);
My question is this: How can I setup my test data for unit tests so that .CreateSourceQuery()
does not return null?