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

Add generic parameter type informat to exception text. #177

Merged
merged 2 commits into from
Jun 8, 2015
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
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
59 changes: 59 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,64 @@ public void DoTest()
#endif
#endregion

#region #176

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

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

try
{
mock.Object.DoSomething<string>(0);
}
catch (MockException exception)
{
var genericTypesRE = new Regex(@"\<.*?\>");
var match = genericTypesRE.Match(exception.Message);

Assert.True(match.Success);
Assert.Equal("<string>", 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<ISomeInterface>(MockBehavior.Strict);
mock.Setup(m => m.DoSomething<int>(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
Expand Down