From 34093106ad7fb4c2ec0c2980e34eddf76e8709db Mon Sep 17 00:00:00 2001 From: James Johnson Date: Sun, 7 Jun 2015 01:22:01 -0400 Subject: [PATCH 1/2] Adds additional generic parameter to exception message Adds type information for a generic method to the exception output, making it easier to debug issues where passed arguments may match, but the generic parameters do not. Fixes #176 --- Source/Extensions.cs | 6 +++- UnitTests/Regressions/IssueReportsFixture.cs | 30 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) 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..90ff05656 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,35 @@ public void DoTest() #endif #endregion + #region #176 + public class Issue176 + { + public interface ISomeInterface + { + TResult DoSomething(int anInt); + } + + [Fact] + public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters() + { + var mock = new Mock(); + 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); + } + } + } + #endregion // #176 + // Old @ Google Code #region #47 From 26bbd829b39ad9ecf6076bf552c9f280e46f0e5d Mon Sep 17 00:00:00 2001 From: James Johnson Date: Sun, 7 Jun 2015 01:33:56 -0400 Subject: [PATCH 2/2] Adds tests for #176 --- UnitTests/Regressions/IssueReportsFixture.cs | 81 +++++++++++++------- 1 file changed, 55 insertions(+), 26 deletions(-) diff --git a/UnitTests/Regressions/IssueReportsFixture.cs b/UnitTests/Regressions/IssueReportsFixture.cs index 90ff05656..23ee89414 100644 --- a/UnitTests/Regressions/IssueReportsFixture.cs +++ b/UnitTests/Regressions/IssueReportsFixture.cs @@ -177,32 +177,61 @@ public void DoTest() #endregion #region #176 - public class Issue176 - { - public interface ISomeInterface - { - TResult DoSomething(int anInt); - } - - [Fact] - public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters() - { - var mock = new Mock(); - 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); - } - } - } + + 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