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

Don't throw away generic type arguments in one mock.Protected().Verify<T>() method overload #1325

Merged
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
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