Skip to content

Commit 2802f12

Browse files
committed
Merge pull request #177 from hasaki/add-generic-type-to-exception
Add generic parameter type informat to exception text.
2 parents 8bc9b11 + 26bbd82 commit 2802f12

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

Source/Extensions.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,12 @@ public static string Format(this ICallContext invocation)
8181
return invocation.Method.DeclaringType.Name + "." +
8282
invocation.Method.Name.Substring(4) + " = " + GetValue(invocation.Arguments.First());
8383
}
84+
85+
var genericParameters = invocation.Method.IsGenericMethod
86+
? "<" + string.Join(", ", invocation.Method.GetGenericArguments().Select(t => t.Name)) + ">"
87+
: "";
8488

85-
return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + "(" +
89+
return invocation.Method.DeclaringType.Name + "." + invocation.Method.Name + genericParameters + "(" +
8690
string.Join(", ", invocation.Arguments.Select(a => GetValue(a)).ToArray()) + ")";
8791
}
8892

UnitTests/Regressions/IssueReportsFixture.cs

+59
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Linq.Expressions;
88
using System.Reflection;
99
using System.Text;
10+
using System.Text.RegularExpressions;
1011
using Moq;
1112
using Moq.Properties;
1213
using Moq.Protected;
@@ -175,6 +176,64 @@ public void DoTest()
175176
#endif
176177
#endregion
177178

179+
#region #176
180+
181+
public class Issue176
182+
{
183+
public interface ISomeInterface
184+
{
185+
TResult DoSomething<TResult>(int anInt);
186+
int DoSomethingElse(int anInt);
187+
}
188+
189+
[Fact]
190+
public void when_a_mock_doesnt_match_generic_parameters_exception_indicates_generic_parameters()
191+
{
192+
var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
193+
mock.Setup(m => m.DoSomething<int>(0)).Returns(1);
194+
195+
try
196+
{
197+
mock.Object.DoSomething<string>(0);
198+
}
199+
catch (MockException exception)
200+
{
201+
var genericTypesRE = new Regex(@"\<.*?\>");
202+
var match = genericTypesRE.Match(exception.Message);
203+
204+
Assert.True(match.Success);
205+
Assert.Equal("<string>", match.Captures[0].Value, StringComparer.OrdinalIgnoreCase);
206+
return;
207+
}
208+
209+
Assert.True(false, "No exception was thrown when one should have been");
210+
}
211+
212+
[Fact]
213+
public void when_a_method_doesnt_have_generic_parameters_exception_doesnt_include_brackets()
214+
{
215+
var mock = new Mock<ISomeInterface>(MockBehavior.Strict);
216+
mock.Setup(m => m.DoSomething<int>(0)).Returns(1);
217+
218+
try
219+
{
220+
mock.Object.DoSomethingElse(0);
221+
}
222+
catch (MockException exception)
223+
{
224+
var genericTypesRE = new Regex(@"\<.*?\>");
225+
var match = genericTypesRE.Match(exception.Message);
226+
227+
Assert.False(match.Success);
228+
return;
229+
}
230+
231+
Assert.True(false, "No exception was thrown when one should have been");
232+
}
233+
}
234+
235+
#endregion // #176
236+
178237
// Old @ Google Code
179238

180239
#region #47

0 commit comments

Comments
 (0)