Skip to content

Remove Mock.DelegateInterfaceMethod #705

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/Moq/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,13 @@ internal static (Expression Object, MethodInfo Method, IReadOnlyList<Expression>
{
// We're a mock for a delegate, so this call can only
// possibly be the result of invoking the delegate.
var invocation = (InvocationExpression)expression.Body;

// But the expression we have is for a call on the delegate, not our
// delegate interface proxy, so we need to map instead to the
// method on that interface, which is the property we've just tested for.
var invocation = (InvocationExpression)expression.Body;
return (Object: invocation.Expression, Method: mock.DelegateInterfaceMethod, Arguments: invocation.Arguments);
_ = ProxyFactory.Instance.GetDelegateProxyInterface(mock.TargetType, out var delegateInterfaceMethod);
return (Object: invocation.Expression, Method: delegateInterfaceMethod, Arguments: invocation.Arguments);
}

if (expression.Body is MethodCallExpression methodCall)
Expand Down
22 changes: 1 addition & 21 deletions src/Moq/Mock.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -210,11 +210,6 @@ public override string ToString()
return this.Name;
}

internal override bool IsDelegateMock
{
get { return typeof(T).IsDelegate(); }
}

[DebuggerStepThrough]
private void InitializeInstance()
{
Expand All @@ -234,7 +229,7 @@ private void InitializeInstancePexProtected()
// We're mocking a delegate.
// Firstly, get/create an interface with a method whose signature
// matches that of the delegate.
var delegateInterfaceType = ProxyFactory.Instance.GetDelegateProxyInterface(typeof(T), out delegateInterfaceMethod);
var delegateInterfaceType = ProxyFactory.Instance.GetDelegateProxyInterface(typeof(T), out var delegateInterfaceMethod);

// Then create a proxy for that.
var delegateProxy = ProxyFactory.Instance.CreateProxy(
Expand All @@ -257,21 +252,6 @@ private void InitializeInstancePexProtected()
}
}

private MethodInfo delegateInterfaceMethod;

/// <inheritdoc />
internal override MethodInfo DelegateInterfaceMethod
{
get
{
// Ensure object is created, which causes the delegateInterfaceMethod
// to be initialized.
OnGetObject();

return delegateInterfaceMethod;
}
}

/// <summary>
/// Returns the mocked object value.
/// </summary>
Expand Down
14 changes: 1 addition & 13 deletions src/Moq/Mock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,7 @@ public DefaultValue DefaultValue
/// </summary>
internal abstract Type MockedType { get; }

/// <summary>
/// If this is a mock of a delegate, this property contains the method
/// on the autogenerated interface so that we can convert setup + verify
/// expressions on the delegate into expressions on the interface proxy.
/// </summary>
internal abstract MethodInfo DelegateInterfaceMethod { get; }

/// <summary>
/// Allows to check whether expression conversion to the <see cref="DelegateInterfaceMethod"/>
/// must be performed on the mock, without causing unnecessarily early initialization of
/// the mock instance, which breaks As{T}.
/// </summary>
internal abstract bool IsDelegateMock { get; }
internal bool IsDelegateMock => this.TargetType.IsDelegate();

/// <summary>
/// Gets or sets the <see cref="DefaultValueProvider"/> instance that will be used
Expand Down
11 changes: 8 additions & 3 deletions tests/Moq.Tests/MockedDelegatesFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,18 @@ public void CanSubscribeMockDelegateAsEventListener()
public void DelegateInterfacesAreReused()
{
// White box test: we want to ensure that we're not creating new proxy interfaces
// every time we come across a particular delegate type. Specifically,
// every time we come across a particular delegate type.
var interface1 = ProxyFactory.Instance.GetDelegateProxyInterface(typeof(PropertyChangedEventHandler), out var interfaceMethod1);
var interface2 = ProxyFactory.Instance.GetDelegateProxyInterface(typeof(PropertyChangedEventHandler), out var interfaceMethod2);
Assert.Same(interface1, interface2);
Assert.Same(interfaceMethod1, interfaceMethod2);

// Specifically,
// it's good if multiple mocks for the same delegate (interface) both
// consider themselves to be proxying for the same method.
var mock1 = new Mock<PropertyChangedEventHandler>();
var mock2 = new Mock<PropertyChangedEventHandler>();

Assert.Same(mock1.DelegateInterfaceMethod, mock2.DelegateInterfaceMethod);
Assert.Same(mock1.Object.Method, mock2.Object.Method);
}

[Fact]
Expand Down