Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Mocking DbSet<T>
public static class DbSetInitializer
{
public static void InitDb<T>(IQueryable<T> set, IQueryable<T> data) where T : class
{
set.Provider.Returns(data.Provider);
set.Expression.Returns(data.Expression);
set.ElementType.Returns(data.ElementType);
set.GetEnumerator().Returns(data.GetEnumerator());
set.AsNoTracking().Returns(data);
}
public static void InitDb<T>(DbSet<T> set, List<T> data) where T : class
{
InitDb(set, data.AsQueryable());
set.When(x => x.Add(Arg.Any<T>())).Do(x => data.Add((T)x.Args()[0]));
set.When(x => x.AddAsync(Arg.Any<T>())).Do(x => data.Add((T)x.Args()[0]));
set.When(x => x.AddRangeAsync(Arg.Any<IEnumerable<T>>())).Do(x => data.AddRange((IEnumerable<T>)x.Args()[0]));
set.When(x => x.AddRange(Arg.Any<IEnumerable<T>>())).Do(x => data.AddRange((IEnumerable<T>)x.Args()[0]));
}
}
// Usage:
var list = new List<MyClass>(){
new MyClass(),new MyClass(),new MyClass()
};
var db = Substitute.For<DbSet<MyClass>, IQueryable<MyClass>>();
DbSetInitializer.InitDb(db, list);
_context.MyClassDb.Returns(db); // MyClassDb is an instance of DbSet<MyClass>
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.