How can I fake an ObjectContext when unit testing? I have created my generic repository that excepts an ObjectContext, faked the ObjectSets, but I can't figure out how to create and pass a fake ObjectContext containing the fake ObjectSets into my repository. My repository accepts a class of type ObjectContext.
Any ideas?
public class FakeObjectSet<T> : IObjectSet<T> where T : class {
HashSet<T> _data;
IQueryable _query;
public FakeObjectSet(){
this._data = new HashSet<T>();
this._query = this._data.AsQueryable();
}
public void AddObject(T Item) {
this._data.Add(Item);
}
public void Attach(T Item) {
this._data.Add(Item);
}
public void DeleteObject(T Item) {
this._data.Remove(Item);
}
public void Detach(T Item) {
this._data.Remove(Item);
}
public IEnumerator<T> GetEnumerator() {
return this._data.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return _data.GetEnumerator();
}
public Type ElementType {
get { return _data.AsQueryable().ElementType; }
}
public Expression Expression {
get { return _data.AsQueryable().Expression; }
}
public IQueryProvider Provider {
get { return _query.Provider; }
}
}
My fake objectset implementation
public class FakeJobSet : FakeObjectSet<Job>{
}
and my fake context class:
public class FakeCentralRepositoryContext{
public FakeCentralRepositoryContext(){
this.Jobs = new FakeJobSet();
}
public IObjectSet<Job> Jobs
{
get; private set;
}
}