diff --git a/Source/MethodCall.cs b/Source/MethodCall.cs index 06a251df6..f0eac0d89 100644 --- a/Source/MethodCall.cs +++ b/Source/MethodCall.cs @@ -81,7 +81,20 @@ public IVerifies Raises(Action eventExpression, params object[] args) } } - internal partial class MethodCall : IProxyCall, ICallbackResult, IVerifies, IThrowsResult + internal class TypeEqualityComparer : IEqualityComparer + { + 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; @@ -95,8 +108,9 @@ internal partial class MethodCall : IProxyCall, ICallbackResult, IVerifies, IThr private int? expectedCallCount = null; protected Func condition; private List> outValues = new List>(); + private static readonly IEqualityComparer typesComparer = new TypeEqualityComparer(); - public MethodCall(Mock mock, Func condition, Expression originalExpression, MethodInfo method, params Expression[] arguments) + public MethodCall(Mock mock, Func condition, Expression originalExpression, MethodInfo method, params Expression[] arguments) { this.Mock = mock; this.condition = condition; @@ -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; } diff --git a/UnitTests/VerifyFixture.cs b/UnitTests/VerifyFixture.cs index 10365f597..7c3c29f04 100644 --- a/UnitTests/VerifyFixture.cs +++ b/UnitTests/VerifyFixture.cs @@ -866,6 +866,15 @@ public void IncludesMessageAboutNoActualCallsInFailureMessage() Assert.Contains(Environment.NewLine + "No invocations performed.", mex.Message); } + [Fact] + public void MatchesDerivedTypesForGenericTypes() + { + var mock = new Mock(); + mock.Object.Call(new BazParam()); + mock.Object.Call(new BazParam2()); + + mock.Verify(foo => foo.Call(It.IsAny()), Times.Exactly(2)); + } public interface IBar { @@ -882,6 +891,23 @@ public interface IFoo void Submit(); string Execute(string command); } + + public interface IBazParam + { + } + + public interface IBaz + { + void Call(T param) where T:IBazParam; + } + + public class BazParam:IBazParam + { + } + + public class BazParam2:BazParam + { + } } }