Skip to content

Instantly share code, notes, and snippets.

@LukeWinikates
Created October 24, 2011 16:31
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save LukeWinikates/1309447 to your computer and use it in GitHub Desktop.
Save LukeWinikates/1309447 to your computer and use it in GitHub Desktop.
An implementation of IDbSet to help with mocking Entity Framework DbContexts.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EntityExtensions {
public class FakeDbSet<T> : System.Data.Entity.IDbSet<T> where T : class {
private readonly List<T> list = new List<T>();
public FakeDbSet() {
list = new List<T>();
}
public FakeDbSet(IEnumerable<T> contents) {
this.list = contents.ToList();
}
#region IDbSet<T> Members
public T Add(T entity) {
this.list.Add(entity);
return entity;
}
public T Attach(T entity) {
this.list.Add(entity);
return entity;
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T {
throw new NotImplementedException();
}
public T Create() {
throw new NotImplementedException();
}
public T Find(params object[] keyValues) {
throw new NotImplementedException();
}
public System.Collections.ObjectModel.ObservableCollection<T> Local {
get {
throw new NotImplementedException();
}
}
public T Remove(T entity) {
this.list.Remove(entity);
return entity;
}
#endregion
#region IEnumerable<T> Members
public IEnumerator<T> GetEnumerator() {
return this.list.GetEnumerator();
}
#endregion
#region IEnumerable Members
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
return this.list.GetEnumerator();
}
#endregion
#region IQueryable Members
public Type ElementType {
get { return this.list.AsQueryable().ElementType; }
}
public System.Linq.Expressions.Expression Expression {
get { return this.list.AsQueryable().Expression; }
}
public IQueryProvider Provider {
get { return this.list.AsQueryable().Provider; }
}
#endregion
}
}
@Damovisa
Copy link

Perfect, just what I needed, thanks :)

@deepakquovantis
Copy link

Thanks :-)

@noscogeorge280
Copy link

Thank it really perfect

@anth12
Copy link

anth12 commented Aug 26, 2016

Very good, thanks

@kiquenet
Copy link

@Marceloromeugoncalves
Copy link

Very good. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment