Skip to content

Commit 1c4c723

Browse files
authored
Merge pull request #1325 from stakx/bugfix/verify-protected-generic-non-void-method
Don't throw away generic type arguments in one `mock.Protected().Verify<T>()` method overload
2 parents bc34f8e + 4240ff4 commit 1c4c723

File tree

3 files changed

+34
-1
lines changed

3 files changed

+34
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
1313
* `setup.Verifiable(Times times, [string failMessage])` method to specify the expected number of calls upfront. `mock.Verify[All]` can then be used to check whether the setup was called that many times. The upper bound (maximum allowed number of calls) will be checked right away, i.e. whenever a setup gets called. (@stakx, #1319)
1414
* Add `ThrowsAsync` methods for non-generic `ValueTask` (@johnthcall, #1235)
1515

16+
#### Fixed
17+
18+
* Verifying a protected generic method that returns a value is broken (@nthornton2010, #1314)
19+
20+
1621
## 4.18.4 (2022-12-30)
1722

1823
#### Changed

src/Moq/Protected/ProtectedMock.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ public void Verify<TResult>(string methodName, Times times, bool exactParameterM
253253
public void Verify<TResult>(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
254254
{
255255
Guard.NotNull(genericTypeArguments, nameof(genericTypeArguments));
256-
this.InternalVerify<TResult>(methodName, null, times, exactParameterMatch, args);
256+
this.InternalVerify<TResult>(methodName, genericTypeArguments, times, exactParameterMatch, args);
257257
}
258258

259259
private void InternalVerify<TResult>(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)

tests/Moq.Tests/Regressions/IssueReportsFixture.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4024,6 +4024,34 @@ public interface IX
40244024
}
40254025
}
40264026

4027+
#endregion
4028+
4029+
#region #1314
4030+
4031+
public class Issue1314
4032+
{
4033+
[Fact]
4034+
public void Verify_protected_generic_non_void_method_using_exactParameterMatch_overload_uses_provided_generic_type_arguments()
4035+
{
4036+
var mock = new Mock<C>();
4037+
4038+
_ = mock.Object.InvokeMethod<int>();
4039+
_ = mock.Object.InvokeMethod<bool>();
4040+
_ = mock.Object.InvokeMethod<int>();
4041+
4042+
mock.Protected().Verify<string>("Method", new Type[] { typeof(float) }, Times.Never(), true);
4043+
mock.Protected().Verify<string>("Method", new Type[] { typeof(bool) }, Times.Once(), true);
4044+
mock.Protected().Verify<string>("Method", new Type[] { typeof(int) }, Times.Exactly(2), true);
4045+
}
4046+
4047+
public abstract class C
4048+
{
4049+
protected abstract string Method<T>();
4050+
4051+
public string InvokeMethod<T>() => Method<T>();
4052+
}
4053+
}
4054+
40274055
#endregion
40284056

40294057
// Old @ Google Code

0 commit comments

Comments
 (0)