Created
January 28, 2015 13:55
-
-
Save vintharas/bfa5709746f9dbea3794 to your computer and use it in GitHub Desktop.
Mocking IDBSet IQueryable with Moq
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.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