Skip to content

Instantly share code, notes, and snippets.

@Vintharas
Created January 28, 2015 13:55
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Vintharas/bfa5709746f9dbea3794 to your computer and use it in GitHub Desktop.
Save Vintharas/bfa5709746f9dbea3794 to your computer and use it in GitHub Desktop.
Mocking IDBSet IQueryable with Moq
using System.Data.Entity;
using System.Linq;
using Moq;
namespace Platform.Domain.Tests.Helpers
{
public class QueryableDbSetMock
{
public static IDbSet<T> GetQueryableMockDbSet<T>(params T[] sourceList) where T : class
{
var queryable = sourceList.AsQueryable();
var dbSet = new Mock<IDbSet<T>>();
dbSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(queryable.Provider);
dbSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(queryable.Expression);
dbSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(queryable.ElementType);
dbSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(queryable.GetEnumerator());
return dbSet.Object;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment