Skip to content

Commit

Permalink
Merge pull request #842 from stakx/strict-linq-to-mocks
Browse files Browse the repository at this point in the history
Add LINQ to Mocks support for strict mocks
  • Loading branch information
stakx authored Jun 2, 2019
2 parents 8eb41b3 + 5a65fe3 commit c5a2465
Show file tree
Hide file tree
Showing 8 changed files with 246 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
#### Added

* New method overload `sequenceSetup.ReturnsAsync(Func<T>)` (@stakx, #841)
* LINQ to Mocks support for strict mocks, i.e. new method overloads for `Mock.Of`, `Mocks.Of`, `mockRepository.Of`, and `mockRepository.OneOf` that accept a `MockBehavior` parameter. (@stakx, #842)

#### Fixed

Expand Down
33 changes: 30 additions & 3 deletions src/Moq/Linq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,29 @@ public partial class Mock
/// <typeparam name="T">The type of the mocked object.</typeparam>
/// <returns>The mocked object created.</returns>
public static T Of<T>() where T : class
{
return Mock.Of<T>(MockBehavior.Default);
}

/// <summary>
/// Creates an mock object of the indicated type.
/// </summary>
/// <param name="behavior">Behavior of the mock.</param>
/// <typeparam name="T">The type of the mocked object.</typeparam>
/// <returns>The mocked object created.</returns>
public static T Of<T>(MockBehavior behavior) where T : class
{
// This method was originally implemented as follows:
//
// return Mocks.CreateMockQuery<T>().First<T>();
//
// 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>();
mock.SetupAllProperties();
var mock = new Mock<T>(behavior);
if (behavior != MockBehavior.Strict)
{
mock.SetupAllProperties();
}
return mock.Object;
}

Expand All @@ -40,7 +54,20 @@ public static T Of<T>() where T : class
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By Design")]
public static T Of<T>(Expression<Func<T, bool>> predicate) where T : class
{
var mocked = Mocks.CreateMockQuery<T>().First<T>(predicate);
return Mock.Of<T>(predicate, MockBehavior.Default);
}

/// <summary>
/// Creates an mock object of the indicated type.
/// </summary>
/// <param name="predicate">The predicate with the specification of how the mocked object should behave.</param>
/// <param name="behavior">Behavior of the mock.</param>
/// <typeparam name="T">The type of the mocked object.</typeparam>
/// <returns>The mocked object created.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By Design")]
public static T Of<T>(Expression<Func<T, bool>> predicate, MockBehavior behavior) where T : class
{
var mocked = Mocks.CreateMockQuery<T>(behavior).First(predicate);

// The current implementation of LINQ to Mocks creates mocks that already have recorded invocations.
// Because this interferes with `VerifyNoOtherCalls`, we recursively clear all invocations before
Expand Down
79 changes: 65 additions & 14 deletions src/Moq/Linq/MockRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,18 @@ public partial class MockRepository
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
public IQueryable<T> Of<T>() where T : class
{
return CreateMockQuery<T>();
return this.Of<T>(this.Behavior);
}

/// <summary>
/// Access the universe of mocks of the given type, to retrieve those
/// that behave according to the LINQ query specification.
/// </summary>
/// <param name="behavior">Behavior of the mocks.</param>
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
public IQueryable<T> Of<T>(MockBehavior behavior) where T : class
{
return this.CreateMockQuery<T>(behavior);
}

/// <summary>
Expand All @@ -37,7 +48,20 @@ public IQueryable<T> Of<T>() where T : class
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By design")]
public IQueryable<T> Of<T>(Expression<Func<T, bool>> specification) where T : class
{
return CreateMockQuery<T>().Where(specification);
return this.Of<T>(specification, this.Behavior);
}

/// <summary>
/// Access the universe of mocks of the given type, to retrieve those
/// that behave according to the LINQ query specification.
/// </summary>
/// <param name="specification">The predicate with the setup expressions.</param>
/// <param name="behavior">Behavior of the mocks.</param>
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By design")]
public IQueryable<T> Of<T>(Expression<Func<T, bool>> specification, MockBehavior behavior) where T : class
{
return this.CreateMockQuery<T>(behavior).Where(specification);
}

/// <summary>
Expand All @@ -48,7 +72,19 @@ public IQueryable<T> Of<T>(Expression<Func<T, bool>> specification) where T : cl
[EditorBrowsable(EditorBrowsableState.Never)]
public T OneOf<T>() where T : class
{
return CreateMockQuery<T>().First<T>();
return this.OneOf<T>(this.Behavior);
}

/// <summary>
/// Creates an mock object of the indicated type.
/// </summary>
/// <param name="behavior">Behavior of the mock.</param>
/// <typeparam name="T">The type of the mocked object.</typeparam>
/// <returns>The mocked object created.</returns>
[EditorBrowsable(EditorBrowsableState.Never)]
public T OneOf<T>(MockBehavior behavior) where T : class
{
return this.CreateMockQuery<T>(behavior).First();
}

/// <summary>
Expand All @@ -61,39 +97,54 @@ public T OneOf<T>() where T : class
[EditorBrowsable(EditorBrowsableState.Never)]
public T OneOf<T>(Expression<Func<T, bool>> specification) where T : class
{
return CreateMockQuery<T>().First<T>(specification);
return this.OneOf<T>(specification, this.Behavior);
}

/// <summary>
/// Creates an mock object of the indicated type.
/// </summary>
/// <param name="specification">The predicate with the setup expressions.</param>
/// <param name="behavior">Behavior of the mock.</param>
/// <typeparam name="T">The type of the mocked object.</typeparam>
/// <returns>The mocked object created.</returns>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By Design")]
[EditorBrowsable(EditorBrowsableState.Never)]
public T OneOf<T>(Expression<Func<T, bool>> specification, MockBehavior behavior) where T : class
{
return this.CreateMockQuery<T>(behavior).First(specification);
}

/// <summary>
/// Creates the mock query with the underlying queryable implementation.
/// </summary>
internal IQueryable<T> CreateMockQuery<T>() where T : class
internal IQueryable<T> CreateMockQuery<T>(MockBehavior behavior) where T : class
{
var method = ((Func<IQueryable<T>>)CreateQueryable<T>).GetMethodInfo();
return new MockQueryable<T>(Expression.Call(
Expression.Constant(this),
method));
var method = ((Func<MockBehavior, IQueryable<T>>)CreateQueryable<T>).GetMethodInfo();
return new MockQueryable<T>(Expression.Call(Expression.Constant(this), method, Expression.Constant(behavior)));
}

/// <summary>
/// Wraps the enumerator inside a queryable.
/// </summary>
internal IQueryable<T> CreateQueryable<T>() where T : class
internal IQueryable<T> CreateQueryable<T>(MockBehavior behavior) where T : class
{
return CreateMocks<T>().AsQueryable();
return this.CreateMocks<T>(behavior).AsQueryable();
}

/// <summary>
/// Method that is turned into the actual call from .Query{T}, to
/// transform the queryable query into a normal enumerable query.
/// This method is never used directly by consumers.
/// </summary>
private IEnumerable<T> CreateMocks<T>() where T : class
private IEnumerable<T> CreateMocks<T>(MockBehavior behavior) where T : class
{
do
{
var mock = this.Create<T>();
mock.SetupAllProperties();
var mock = this.Create<T>(behavior);
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
73 changes: 61 additions & 12 deletions src/Moq/Linq/Mocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,18 @@ public static class Mocks
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
public static IQueryable<T> Of<T>() where T : class
{
return CreateMockQuery<T>();
return Mocks.Of<T>(MockBehavior.Default);
}

/// <summary>
/// Access the universe of mocks of the given type, to retrieve those
/// that behave according to the LINQ query specification.
/// </summary>
/// <param name="behavior">Behavior of the mocks.</param>
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
public static IQueryable<T> Of<T>(MockBehavior behavior) where T : class
{
return Mocks.CreateMockQuery<T>(behavior);
}

/// <summary>
Expand All @@ -38,7 +49,20 @@ public static IQueryable<T> Of<T>() where T : class
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By design")]
public static IQueryable<T> Of<T>(Expression<Func<T, bool>> specification) where T : class
{
return CreateMockQuery<T>().Where(specification);
return Mocks.Of<T>(specification, MockBehavior.Default);
}

/// <summary>
/// Access the universe of mocks of the given type, to retrieve those
/// that behave according to the LINQ query specification.
/// </summary>
/// <param name="specification">The predicate with the setup expressions.</param>
/// <param name="behavior">Behavior of the mocks.</param>
/// <typeparam name="T">The type of the mocked object to query.</typeparam>
[SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures", Justification = "By design")]
public static IQueryable<T> Of<T>(Expression<Func<T, bool>> specification, MockBehavior behavior) where T : class
{
return Mocks.CreateMockQuery<T>(behavior).Where(specification);
}

/// <summary>
Expand Down Expand Up @@ -70,32 +94,34 @@ public static T OneOf<T>(Expression<Func<T, bool>> specification) where T : clas
/// <summary>
/// Creates the mock query with the underlying queryable implementation.
/// </summary>
internal static IQueryable<T> CreateMockQuery<T>() where T : class
internal static IQueryable<T> CreateMockQuery<T>(MockBehavior behavior) where T : class
{
var method = ((Func<IQueryable<T>>)CreateQueryable<T>).GetMethodInfo();
return new MockQueryable<T>(Expression.Call(null,
method));
var method = ((Func<MockBehavior, IQueryable<T>>)CreateQueryable<T>).GetMethodInfo();
return new MockQueryable<T>(Expression.Call(method, Expression.Constant(behavior)));
}

/// <summary>
/// Wraps the enumerator inside a queryable.
/// </summary>
internal static IQueryable<T> CreateQueryable<T>() where T : class
internal static IQueryable<T> CreateQueryable<T>(MockBehavior behavior) where T : class
{
return CreateMocks<T>().AsQueryable();
return Mocks.CreateMocks<T>(behavior).AsQueryable();
}

/// <summary>
/// Method that is turned into the actual call from .Query{T}, to
/// transform the queryable query into a normal enumerable query.
/// This method is never used directly by consumers.
/// </summary>
private static IEnumerable<T> CreateMocks<T>() where T : class
private static IEnumerable<T> CreateMocks<T>(MockBehavior behavior) where T : class
{
do
{
var mock = new Mock<T>();
mock.SetupAllProperties();
var mock = new Mock<T>(behavior);
if (behavior != MockBehavior.Strict)
{
mock.SetupAllProperties();
}

yield return mock.Object;
}
Expand All @@ -112,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
5 changes: 5 additions & 0 deletions src/Moq/Obsolete/MockFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,11 @@ public MockFactory(MockBehavior defaultBehavior)
this.switches = Switches.Default;
}

/// <summary>
/// Gets the default <see cref="MockBehavior"/> of mocks created by this repository.
/// </summary>
internal MockBehavior Behavior => this.defaultBehavior;

/// <summary>
/// Whether the base member virtual implementation will be called
/// for mocked classes if no setup is matched. Defaults to <see langword="false"/>.
Expand Down
49 changes: 49 additions & 0 deletions tests/Moq.Tests/Linq/MockRepositoryQuerying.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,60 @@ public void WhenQueryingMultipleWithProperty_ThenItIsStrict()

Assert.Equal("1", foo.Id);
}

[Fact]
public void Can_override_behavior_and_create_loose_mock()
{
var foo = this.repository.OneOf<IFoo>(MockBehavior.Loose);
Assert.Equal(MockBehavior.Loose, Mock.Get(foo).Behavior);
_ = foo.Id;
}
}

public class Strict_mocks
{
private MockRepository repository;

public Strict_mocks()
{
this.repository = new MockRepository(MockBehavior.Default);
}

[Fact]
public void Strict_Of_will_throw_for_non_setup_property()
{
var foo = this.repository.Of<IFoo>(MockBehavior.Strict).First();
Assert.Throws<MockException>(() => _ = foo.Name);
}

[Fact]
public void Strict_Of_with_expression_will_throw_for_non_setup_property()
{
var foo = this.repository.Of<IFoo>(f => f.Id == default, MockBehavior.Strict).First();
_ = foo.Id;
Assert.Throws<MockException>(() => _ = foo.Name);
}

[Fact]
public void Strict_OneOf_will_throw_for_non_setup_property()
{
var foo = this.repository.OneOf<IFoo>(MockBehavior.Strict);
Assert.Throws<MockException>(() => _ = foo.Name);
}

[Fact]
public void Strict_OneOf_with_expression_will_throw_for_non_setup_property()
{
var foo = this.repository.OneOf<IFoo>(f => f.Id == default, MockBehavior.Strict);
_ = foo.Id;
Assert.Throws<MockException>(() => _ = foo.Name);
}
}

public interface IFoo
{
string Id { get; set; }
string Name { get; set; }
bool Do();
}
}
Expand Down
Loading

0 comments on commit c5a2465

Please sign in to comment.