Skip to content
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

Corrected Verify method behavior for generic methods calls #25

Merged
merged 3 commits into from
Sep 21, 2012
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
21 changes: 18 additions & 3 deletions Source/MethodCall.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,20 @@ public IVerifies Raises(Action<TMock> eventExpression, params object[] args)
}
}

internal partial class MethodCall : IProxyCall, ICallbackResult, IVerifies, IThrowsResult
internal class TypeEqualityComparer : IEqualityComparer<Type>
{
public bool Equals(Type x, Type y)
{
return y.IsAssignableFrom(x);
}

public int GetHashCode(Type obj)
{
return obj.GetHashCode();
}
}

internal partial class MethodCall : IProxyCall, ICallbackResult, IVerifies, IThrowsResult
{
// Internal for AsMockExtensions
private Expression originalExpression;
Expand All @@ -95,8 +108,9 @@ internal partial class MethodCall : IProxyCall, ICallbackResult, IVerifies, IThr
private int? expectedCallCount = null;
protected Func<bool> condition;
private List<KeyValuePair<int, object>> outValues = new List<KeyValuePair<int, object>>();
private static readonly IEqualityComparer<Type> typesComparer = new TypeEqualityComparer();

public MethodCall(Mock mock, Func<bool> condition, Expression originalExpression, MethodInfo method, params Expression[] arguments)
public MethodCall(Mock mock, Func<bool> condition, Expression originalExpression, MethodInfo method, params Expression[] arguments)
{
this.Mock = mock;
this.condition = condition;
Expand Down Expand Up @@ -363,12 +377,13 @@ private bool IsEqualMethodOrOverride(ICallContext call)
{
if (!this.Method.Name.Equals(call.Method.Name, StringComparison.Ordinal) ||
this.Method.ReturnType != call.Method.ReturnType ||
!this.Method.IsGenericMethod &&
!call.Method.GetParameterTypes().SequenceEqual(this.Method.GetParameterTypes()))
{
return false;
}

if (Method.IsGenericMethod && !call.Method.GetGenericArguments().SequenceEqual(Method.GetGenericArguments()))
if (Method.IsGenericMethod && !call.Method.GetGenericArguments().SequenceEqual(Method.GetGenericArguments(),typesComparer))
{
return false;
}
Expand Down
26 changes: 26 additions & 0 deletions UnitTests/VerifyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,15 @@ public void IncludesMessageAboutNoActualCallsInFailureMessage()
Assert.Contains(Environment.NewLine + "No invocations performed.", mex.Message);
}

[Fact]
public void MatchesDerivedTypesForGenericTypes()
{
var mock = new Mock<IBaz>();
mock.Object.Call(new BazParam());
mock.Object.Call(new BazParam2());

mock.Verify(foo => foo.Call(It.IsAny<IBazParam>()), Times.Exactly(2));
}

public interface IBar
{
Expand All @@ -882,6 +891,23 @@ public interface IFoo
void Submit();
string Execute(string command);
}

public interface IBazParam
{
}

public interface IBaz
{
void Call<T>(T param) where T:IBazParam;
}

public class BazParam:IBazParam
{
}

public class BazParam2:BazParam
{
}
}
}

Expand Down