Last active
November 15, 2016 15:16
-
-
Save sharpjs/6595426 to your computer and use it in GitHub Desktop.
How to fake a DbSet<T> for Entity Framework (EF5 as of this writing)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace SharpJS.Data.Entity.Testing | |
{ | |
public abstract class FakeDbContext | |
{ | |
protected FakeDbContext() { } | |
public abstract void SaveChanges (); | |
public abstract void Dispose (); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Data; | |
using System.Data.Entity; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace SharpJS.Data.Entity.Testing | |
{ | |
public abstract class FakeDbSet<TEntity> : IDbSet<TEntity> | |
where TEntity : class | |
{ | |
private readonly List<TEntity> _entities; | |
private readonly IQueryable<TEntity> _query; | |
protected FakeDbSet() | |
{ | |
_entities = new List<TEntity>(); | |
_query = _entities.AsQueryable(); | |
} | |
public List<TEntity> Entities | |
{ | |
get { return _entities; } | |
} | |
Type IQueryable.ElementType | |
{ | |
get { return _query.ElementType; } | |
} | |
Expression IQueryable.Expression | |
{ | |
get { return _query.Expression; } | |
} | |
IQueryProvider IQueryable.Provider | |
{ | |
get { return _query.Provider; } | |
} | |
IEnumerator<TEntity> IEnumerable<TEntity>.GetEnumerator() | |
{ | |
return _entities.GetEnumerator(); | |
} | |
IEnumerator IEnumerable.GetEnumerator() | |
{ | |
return _entities.GetEnumerator(); | |
} | |
public abstract ObservableCollection<TEntity> Local { get; } | |
public abstract TEntity Create(); | |
public abstract TDerivedEntity Create<TDerivedEntity>() | |
where TDerivedEntity : class, TEntity; | |
public abstract TEntity Find (params object[] keyValues); | |
public abstract TEntity Attach (TEntity entity); | |
public abstract TEntity Add (TEntity entity); | |
public abstract TEntity Remove (TEntity entity); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq.Expressions; | |
using Moq; | |
namespace SharpJS.Data.Entity.Testing | |
{ | |
public static class FakeExtensions | |
{ | |
public static TRepository Containing<TRepository, TEntity> | |
(this TRepository repository, params TEntity[] entities) | |
where TRepository : FakeDbSet<TEntity> | |
where TEntity : class | |
{ | |
repository.Entities.AddRange(entities); | |
return repository; | |
} | |
public static TRepository Containing<TRepository, TEntity> | |
(this TRepository repository, IEnumerable<TEntity> entities) | |
where TRepository : FakeDbSet<TEntity> | |
where TEntity : class | |
{ | |
repository.Entities.AddRange(entities); | |
return repository; | |
} | |
public static FakeDbSet<TEntity> HavingRepository<TContext, TEntity> | |
(this TContext context, Expression<Func<TContext, FakeDbSet<TEntity>>> dbSet) | |
where TContext : FakeDbContext | |
where TEntity : class | |
{ | |
FakeDbSet<TEntity> entity = Mock.Of<FakeDbSet<TEntity>>(); | |
Mock.Get(context) | |
.Setup(dbSet) | |
.Returns(entity); | |
return entity; | |
} | |
public static void Creating<TEntity> | |
(this FakeDbSet<TEntity> dbSet, IEnumerable<TEntity> entities) | |
where TEntity : class | |
{ | |
var queue = new Queue<TEntity>(entities); | |
Mock.Get(dbSet) | |
.Setup(c => c.Create()) | |
.Returns(() => queue.Dequeue()); | |
} | |
public static void Creating<TEntity> | |
(this FakeDbSet<TEntity> dbSet, params TEntity[] entities) | |
where TEntity : class | |
{ | |
Creating(dbSet, (IEnumerable<TEntity>)entities); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment