Skip to content

Commit

Permalink
Merge pull request #1325 from stakx/bugfix/verify-protected-generic-n…
Browse files Browse the repository at this point in the history
…on-void-method

Don't throw away generic type arguments in one `mock.Protected().Verify<T>()` method overload
  • Loading branch information
stakx committed Jan 13, 2023
2 parents bc34f8e + 4240ff4 commit 1c4c723
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
* `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)
* Add `ThrowsAsync` methods for non-generic `ValueTask` (@johnthcall, #1235)

#### Fixed

* Verifying a protected generic method that returns a value is broken (@nthornton2010, #1314)


## 4.18.4 (2022-12-30)

#### Changed
Expand Down
2 changes: 1 addition & 1 deletion src/Moq/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ public void Verify<TResult>(string methodName, Times times, bool exactParameterM
public void Verify<TResult>(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
{
Guard.NotNull(genericTypeArguments, nameof(genericTypeArguments));
this.InternalVerify<TResult>(methodName, null, times, exactParameterMatch, args);
this.InternalVerify<TResult>(methodName, genericTypeArguments, times, exactParameterMatch, args);
}

private void InternalVerify<TResult>(string methodName, Type[] genericTypeArguments, Times times, bool exactParameterMatch, params object[] args)
Expand Down
28 changes: 28 additions & 0 deletions tests/Moq.Tests/Regressions/IssueReportsFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4024,6 +4024,34 @@ public interface IX
}
}

#endregion

#region #1314

public class Issue1314
{
[Fact]
public void Verify_protected_generic_non_void_method_using_exactParameterMatch_overload_uses_provided_generic_type_arguments()
{
var mock = new Mock<C>();

_ = mock.Object.InvokeMethod<int>();
_ = mock.Object.InvokeMethod<bool>();
_ = mock.Object.InvokeMethod<int>();

mock.Protected().Verify<string>("Method", new Type[] { typeof(float) }, Times.Never(), true);
mock.Protected().Verify<string>("Method", new Type[] { typeof(bool) }, Times.Once(), true);
mock.Protected().Verify<string>("Method", new Type[] { typeof(int) }, Times.Exactly(2), true);
}

public abstract class C
{
protected abstract string Method<T>();

public string InvokeMethod<T>() => Method<T>();
}
}

#endregion

// Old @ Google Code
Expand Down

0 comments on commit 1c4c723

Please sign in to comment.