Skip to content

Commit

Permalink
Implement strict behavior for LINQ to Mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
stakx committed Jun 2, 2019
1 parent 8b469d5 commit a9d7301
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
5 changes: 4 additions & 1 deletion src/Moq/Linq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public static T Of<T>(MockBehavior behavior) where T : class
// which involved a lot of avoidable `IQueryable` query provider overhead and lambda compilation.
// What it really boils down to is this (much faster) code:
var mock = new Mock<T>(behavior);
mock.SetupAllProperties();
if (behavior != MockBehavior.Strict)
{
mock.SetupAllProperties();
}
return mock.Object;
}

Expand Down
5 changes: 4 additions & 1 deletion src/Moq/Linq/MockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ private IEnumerable<T> CreateMocks<T>(MockBehavior behavior) where T : class
do
{
var mock = this.Create<T>(behavior);
mock.SetupAllProperties();
if (behavior != MockBehavior.Strict)
{
mock.SetupAllProperties();
}

yield return mock.Object;
}
Expand Down
8 changes: 5 additions & 3 deletions src/Moq/Linq/MockSetupsBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,11 @@ private static Expression ConvertToSetupProperty(Expression targetObject, Expres
var mockExpression = propertyCall.Object;
var propertyExpression = propertyCall.Arguments.First().StripQuotes();

// Because Mocks.CreateMocks (the underlying implementation of the IQueryable provider
// already sets up all properties as stubs, we can safely just set the value here,
// which also allows the use of this querying capability against plain DTO even
// We can safely just set the value here since `SetProperty` will temporarily enable auto-stubbing
// if the underlying `IQueryable` provider implementation hasn't already enabled it permanently by
// calling `SetupAllProperties`.
//
// This method also enables the use of this querying capability against plain DTO even
// if their properties are not virtual.
var setPropertyMethod = typeof(Mocks)
.GetMethod("SetProperty", BindingFlags.Static | BindingFlags.NonPublic)
Expand Down
30 changes: 28 additions & 2 deletions src/Moq/Linq/Mocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ private static IEnumerable<T> CreateMocks<T>(MockBehavior behavior) where T : cl
do
{
var mock = new Mock<T>(behavior);
mock.SetupAllProperties();
if (behavior != MockBehavior.Strict)
{
mock.SetupAllProperties();
}

yield return mock.Object;
}
Expand All @@ -135,7 +138,30 @@ internal static bool SetProperty<T, TResult>(Mock<T> target, Expression<Func<T,
var memberExpr = (MemberExpression)propertyReference.Body;
var member = (PropertyInfo)memberExpr.Member;

member.SetValue(target.Object, value, null);
// For strict mocks, we haven't called `SetupAllProperties` on the mock being set up.
// Therefore, whenever a property is being initialized, we quickly need to enable auto-stubbing.
//
// (One would think that it would be simpler to perform `SetupAllProperties` at the beginning
// and leave it enabled until the initialized mock is returned to the user. However, transforming
// the LINQ query such that a final disable of auto-stubbing happens is much more difficult!)

var temporaryAutoSetupProperties = target.AutoSetupPropertiesDefaultValueProvider == null;

if (temporaryAutoSetupProperties)
{
target.AutoSetupPropertiesDefaultValueProvider = target.DefaultValueProvider;
}
try
{
member.SetValue(target.Object, value, null);
}
finally
{
if (temporaryAutoSetupProperties)
{
target.AutoSetupPropertiesDefaultValueProvider = null;
}
}

return true;
}
Expand Down

0 comments on commit a9d7301

Please sign in to comment.