Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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); }
}
}
}
@kelvinrferreira

This comment has been minimized.

Copy link

kelvinrferreira commented Mar 20, 2017

Very good, thank you very much for sharing this 👍

@blackmamba85

This comment has been minimized.

Copy link

blackmamba85 commented Feb 19, 2018

Sweet! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.