diff --git a/Source/Extensions.cs b/Source/Extensions.cs index d3842c9fb..0ae4595f1 100644 --- a/Source/Extensions.cs +++ b/Source/Extensions.cs @@ -81,8 +81,12 @@ public static string Format(this ICallContext invocation) return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name.Substring(4) + " = " + GetValue(invocation.Arguments.First()); } + + var genericParameters = invocation.Method.IsGenericMethod + ? "<" + string.Join(", ", invocation.Method.GetGenericArguments().Select(t => t.Name)) + ">" + : ""; - return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + "(" + + return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + genericParameters + "(" + string.Join(", ", invocation.Arguments.Select(a => GetValue(a)).ToArray()) + ")"; } diff --git a/UnitTests/Regressions/IssueReportsFixture.cs b/UnitTests/Regressions/IssueReportsFixture.cs index b08aa670a..23ee89414 100644 --- a/UnitTests/Regressions/IssueReportsFixture.cs +++ b/UnitTests/Regressions/IssueReportsFixture.cs @@ -7,6 +7,7 @@ using System.Linq.Expressions; using System.Reflection; using System.Text; +using System.Text.RegularExpressions; using Moq; using Moq.Properties; using Moq.Protected; @@ -175,6 +176,64 @@ public void DoTest() #endif #endregion + #region #176 + + public class Issue176 + { + public interface ISomeInterface + { + TResult DoSomething(int anInt); + int DoSomethingElse(int anInt); + } + + [Fact] + public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(m => m.DoSomething(0)).Returns(1); + + try + { + mock.Object.DoSomething(0); + } + catch (MockException exception) + { + var genericTypesRE = new Regex(@"\<.*?\>"); + var match = genericTypesRE.Match(exception.Message); + + Assert.True(match.Success); + Assert.Equal("", match.Captures[0].Value, StringComparer.OrdinalIgnoreCase); + return; + } + + Assert.True(false, "No exception was thrown when one should have been"); + } + + [Fact] + public void when_a_method_doesnt_have_generic_parameters_exception_doesnt_include_brackets() + { + var mock = new Mock(MockBehavior.Strict); + mock.Setup(m => m.DoSomething(0)).Returns(1); + + try + { + mock.Object.DoSomethingElse(0); + } + catch (MockException exception) + { + var genericTypesRE = new Regex(@"\<.*?\>"); + var match = genericTypesRE.Match(exception.Message); + + Assert.False(match.Success); + return; + } + + Assert.True(false, "No exception was thrown when one should have been"); + } + } + + #endregion // #176 + // Old @ Google Code #region #47