Skip to content

Commit

Permalink
Adds additional generic parameter to exception message
Browse files Browse the repository at this point in the history
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
  • Loading branch information
hasaki committed Jun 7, 2015
1 parent 8bc9b11 commit 3409310
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Source/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()) + ")";
}

Expand Down
30 changes: 30 additions & 0 deletions UnitTests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -175,6 +176,35 @@ public void DoTest()
#endif
#endregion

#region #176
public class Issue176
{
public interface ISomeInterface
{
TResult DoSomething<TResult>(int anInt);
}

[Fact]
public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters()
{
var mock = new Mock<ISomeInterface>();
mock.Setup(m => m.DoSomething<int>(0)).Returns(1);

try
{
mock.Object.DoSomething<int>(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
Expand Down

0 comments on commit 3409310

Please sign in to comment.