Question

I need my implementation of IObjectSet<T> to be able to validate expressions as the "real" Linq2Entity provider would. Is it possible?

Background:
I've changed my EF model (model-first) to return IObjectSet<T> instead of ObjectSet<T>
So that I can fake my model, and have it return the data I want.

Problem:
I have written an implementation of IObejctSet<T> that is bacally a wrapper for a List<T>.
This is all good - but when I query my implementation of IObjectSet<T> im using LinqToObject - so there is no guarantee that I have valid Linq2Entity expression.

Proposed solution:
use the real In my implementaion i just use List<T>'s provider:

private class IObjectSetUnitTestImplementation<T> : IObjectSet<T> where T : EntityObject
{
  private IQueryable _qry;

  public IObjectSetUnitTestImplementation(List<T> lst)
  {
    _inner = lst;
    _qry = _inner.AsQueryable();
    Expression = _qry.Expression;
    ElementType = _qry.ElementType;
    Provider = _qry.Provider; //<--should be ObjectSet<T>'s provider
  }
  //Rest of implementaion omitted
}

So I tried this:

ObjectContext context = null;
var _qry = new ObjectQuery<T>(typeof(T).Name, context) as IQueryable;
Expression = _qry.Expression;
ElementType = _qry.ElementType;
Provider = _qry.Provider;

But I have no context to feed it with, and even if I had, it would just try to access the database.

Is there any other way?

Was it helpful?

Solution

so there is no guarantee that I have valid Linq2Entity expression"

Congratulations, you've just discovered a reason, why IObjectSet<T>, IRepository<T> and etc. shouldn't be implemented for IQyeryable<T>. And the reason is simple: query, which is valid for one LINQ provider, can be invalid for another one (even if you just change databases from MS SQL to something other).

And there's no generic way to verify those queries.
So, unit-testing isn't a good choice for IQyeryable<T>, but integration testing is a definitely the thing that you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top