Skip to content

Instantly share code, notes, and snippets.

@rally25rs
Created December 18, 2011 02:11
Show Gist options
  • Save rally25rs/1492134 to your computer and use it in GitHub Desktop.
Save rally25rs/1492134 to your computer and use it in GitHub Desktop.
Fake implementation of Entity Framework IDbSet to use for unit testing.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;
namespace Rally25rs.Gist.Data
{
/// <summary>
/// This is an in-memory, List backed implementation of
/// Entity Framework's System.Data.Entity.IDbSet to use
/// for testing.
/// </summary>
/// <typeparam name="T">The type of entity to store.</typeparam>
public class FakeDbSet<T> : IDbSet<T> where T : class
{
private readonly List<T> _data;
public FakeDbSet()
{
_data = new List<T>();
}
public FakeDbSet(params T[] entities)
{
_data = new List<T>(entities);
}
public IEnumerator<T> GetEnumerator()
{
return _data.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return _data.GetEnumerator();
}
public Expression Expression
{
get { return Expression.Constant(_data.AsQueryable()); }
}
public Type ElementType
{
get { return typeof(T); }
}
public IQueryProvider Provider
{
get { return _data.AsQueryable().Provider; }
}
public T Find(params object[] keyValues)
{
throw new NotImplementedException("Wouldn't you rather use Linq .SingleOrDefault()?");
}
public T Add(T entity)
{
_data.Add(entity);
return entity;
}
public T Remove(T entity)
{
_data.Remove(entity);
return entity;
}
public T Attach(T entity)
{
_data.Add(entity);
return entity;
}
public T Create()
{
return Activator.CreateInstance<T>();
}
public TDerivedEntity Create<TDerivedEntity>() where TDerivedEntity : class, T
{
return Activator.CreateInstance<TDerivedEntity>();
}
public ObservableCollection<T> Local
{
get { return new ObservableCollection<T>(_data); }
}
}
}
@blackmamba85
Copy link

Sweet! Thanks!

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