Created
November 28, 2016 19:47
-
-
Save srkirkland/b3311a8ddeeb9f188b0f6662fbaaeb1d to your computer and use it in GitHub Desktop.
Moq extensions to turn queryable set of data into a mocked db set that will return that data
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
public static class MockExtensions | |
{ | |
public static Mock<DbSet<T>> MockDbSet<T>(this IQueryable<T> data) where T : class, new() | |
{ | |
var mockSet = new Mock<DbSet<T>>(); | |
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(data.Provider); | |
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(data.Expression); | |
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(data.ElementType); | |
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(data.GetEnumerator()); | |
return mockSet; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment