Created
July 15, 2014 05:49
-
-
Save maxgherman/48e96139b51535b24283 to your computer and use it in GitHub Desktop.
Entity Framework DbSet NSubsitute Helper
This file contains hidden or 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.Collections.Generic; | |
using System.Data.Entity; | |
using System.Linq; | |
using NSubstitute; | |
namespace ConsoleApplication | |
{ | |
public static class EntityFrameworkHelper | |
{ | |
public static DbSet<R> AsDbSetSubstitute<R>(this IEnumerable<R> data) where R : class | |
{ | |
var dataQuarebale = data.AsQueryable(); | |
var dbSet = Substitute.For<DbSet<R>, IQueryable<R>>(); | |
dbSet.InitializeDbSet<R>(dataQuarebale); | |
return dbSet; | |
} | |
public static void InitializeDbSet<R>(this DbSet<R> dbSet, IQueryable<R> data) where R : class | |
{ | |
var dbSetQuarebale = (IQueryable<R>)dbSet; | |
dbSetQuarebale.Provider.Returns(data.Provider); | |
dbSetQuarebale.Expression.Returns(data.Expression); | |
dbSetQuarebale.ElementType.Returns(data.ElementType); | |
dbSetQuarebale.GetEnumerator().Returns(data.GetEnumerator()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment