Skip to content

Instantly share code, notes, and snippets.

@blackmamba85
Forked from rally25rs/FakeDbSet.cs
Created February 19, 2018 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save blackmamba85/328c8c6c5b7193947cc3135a424976f0 to your computer and use it in GitHub Desktop.
Save blackmamba85/328c8c6c5b7193947cc3135a424976f0 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); }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment