Created
March 7, 2017 17:01
-
-
Save osmyn/6085b1ff7bf4f226eb86461b291213da to your computer and use it in GitHub Desktop.
Mock .Include for EF
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
private void HelpSetupActivitiesFakes( | |
List<Activity> fakeActivity, | |
ActivityOutcome fakeOutcome, | |
List<ArgumentHierarchy> fakeHierarchy) | |
{ | |
var mockedActivity = RepositoryTestingHelper | |
.GetMockDbSet<Activity>(fakeActivity.AsQueryable()); | |
mockedActivity.Setup(x => x.Include("InputArgument")) | |
.Returns(mockedActivity.Object); | |
mockedActivity.Setup(x => x.Include("OutputArgument")) | |
.Returns(mockedActivity.Object); | |
_mockContext.Setup(m => m.Activities) | |
.Returns(mockedActivity.Object); | |
_mockContext.Setup(m => m.ActivityOutcomes) | |
.Returns(RepositoryTestingHelper.GetQueryableMockDbSet(fakeOutcome)); | |
var hierarchySet = new Mock<DbSet<ArgumentHierarchy>>(); | |
hierarchySet.Setup(m => m.SqlQuery(It.IsAny<string>(), It.IsAny<object>())) | |
.Returns<string, object[]>((sql, param) => | |
{ | |
var sqlQueryMock = new Mock<DbSqlQuery<ArgumentHierarchy>>(); | |
sqlQueryMock.Setup(m => m.AsNoTracking()) | |
.Returns(sqlQueryMock.Object); | |
sqlQueryMock.Setup(m => m.GetEnumerator()) | |
.Returns(fakeHierarchy.GetEnumerator()); | |
return sqlQueryMock.Object; | |
}); | |
_mockContext.Setup(m => m.Set<ArgumentHierarchy>()) | |
.Returns(hierarchySet.Object); | |
} | |
[TestMethod] | |
[TestCategory("UnitTest")] | |
public void Activities_WhenTwoActive_ReturnsTwoResults() | |
{ | |
//Arrange | |
var fakeActivity = new List<Activity>{ | |
new Activity { | |
ActivityID = 1 | |
}, | |
new Activity{ | |
ActivityID = 2 | |
} | |
}; | |
var fakeOutcome = Fakes.FakeOutcome(); | |
HelpSetupActivitiesFakes(fakeActivity, fakeOutcome, Fakes.FakeHierarchy()); | |
//act | |
var results = _domainDal.Activities(); | |
//assert | |
Assert.AreEqual(fakeActivity.Count, results.Count); | |
} | |
public static class RepositoryTestingHelper | |
{ | |
public static DbSet<T> GetQueryableMockDbSet<T>(params T[] sourceList) where T : class | |
{ | |
var queryable = sourceList.AsQueryable(); | |
var dbSet = new Mock<DbSet<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; | |
} | |
public static Mock<DbSet<T>> GetMockDbSet<T>(IQueryable<T> entities) where T : class | |
{ | |
var mockSet = new Mock<DbSet<T>>(); | |
mockSet.As<IQueryable<T>>().Setup(m => m.Provider).Returns(entities.Provider); | |
mockSet.As<IQueryable<T>>().Setup(m => m.Expression).Returns(entities.Expression); | |
mockSet.As<IQueryable<T>>().Setup(m => m.ElementType).Returns(entities.ElementType); | |
mockSet.As<IQueryable<T>>().Setup(m => m.GetEnumerator()).Returns(entities.GetEnumerator()); | |
return mockSet; | |
} | |
} |
I didn't see the notifications for your messages here, sorry. @hey001us, you can check out https://msdn.microsoft.com/en-us/data/dn314431 for more info. @DemiDOS, I'm not sure if this works in .net core - I haven't tried it there yet.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Osmyn,
I'm trying to reuse your approach, but it does not work to me. When I mock 'Include' method, I get an error 'Message: System.NotSupportedException : Invalid setup on an extension method: x => x.Include("Tasks")'. My code is below:
var mockedActivity = RepositoryTestingHelper.GetMockDbSet(fakeActivity.AsQueryable());
mockedActivity.Setup(x => x.Include(nameof(Activity.Tasks))).Returns(mockedActivity.Object);
My DbSet is Microsoft.EntityFrameworkCore.DbSet.