8000 Add `mock.Protected().Verify` overloads with additional `exactParameterMatch` parameter by Shereef · Pull Request #753 · devlooped/moq · GitHub
[go: up one dir, main page]

Skip to content
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ The format is loosely based on [Keep a Changelog](http://keepachangelog.com/en/1
This release contains several **minor breaking changes**. Please review your code with respect to the following:

* All custom argument matcher methods (including those using `Match.Create<T>`) must now be marked with the `[Matcher]` attribute. For this reason, `MatcherAttribute` is no longer marked obsolete (@stakx, #732)
* Method overload resolution for `mock.Protected().Setup("VoidMethod", ...)` may change due to a new overload: If you set up a `void` method whose first argument is a `bool`, make sure that argument gets interpreted as part of `args`, not as `exactParameterMatch` (see also *Added* section below). (@stakx, #751)
* Method overload resolution for `mock.Protected().Setup("VoidMethod", ...)`, `mock.Protected().Verify("VoidMethod", ...)` and `mock.Protected().Verify<TResult>("NonVoidMethod", ...)` may change due to a new overloads: If the first argument is a `bool`, make sure that argument gets interpreted as part of `args`, not as `exactParameterMatch` (see also *Added* section below). (@stakx & @Shereef, #751, #753)

#### Added

* New method overload `mock.Protected().Setup("VoidMethod", exactParameterMatch, args)` overload having a `bool exactParameterMatch` parameter. Due to method overload resolution, it was easy to think this already existed resolution when in fact it did not, leading to failing tests. (@stakx, #751)
* New method overloads `mock.Protected().Verify("VoidMethod", times, exactParameterMatch, args)` and `mock.Protected().Verify<TResult>("NonVoidMethod", times, exactParameterMatch, args)` overloads having a `bool exactParameterMatch` parameter. (@Shereef, #753)

#### Fixed

Expand Down
29 changes: 29 additions & 0 deletions src/Moq/Protected/IProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,20 @@ IProtectedAsMock<TMock, TAnalog> As<TAnalog>()
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
void Verify(string methodName, Times times, params object[] args);

/// <summary>
/// Specifies a verify for a void method with the given <paramref name="methodName"/>,
/// optionally specifying arguments for the method call. Use in conjunction with the default
/// <see cref="MockBehavior.Loose"/>.
/// </summary>
/// <exception cref="MockException">The invocation was not call the times specified by
/// <paramref name="times"/>.</exception>
/// <param name="methodName">The name of the void method to be verified.</param>
/// <param name="times">The number of times a method is allowed to be called.</param>
/// <param name="exactParameterMatch">Should the parameter types match exactly types that were provided</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
void Verify(string methodName, Times times, bool exactParameterMatch, params object[] args);
Comment thread
Shereef marked this conversation as resolved.
Resolved

/// <summary>
/// Specifies a verify for an invocation on a property or a non void method with the given
/// <paramref name="methodName"/>, optionally specifying arguments for the method call.
Expand All @@ -154,6 +168,21 @@ IProtectedAsMock<TMock, TAnalog> As<TAnalog>()
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
void Verify<TResult>(string methodName, Times times, params object[] args);

/// <summary>
/// Specifies a verify for an invocation on a property or a non void method with the given
/// <paramref name="methodName"/>, optionally specifying arguments for the method call.
/// </summary>
/// <exception cref="MockException">The invocation was not call the times specified by
/// <paramref name="times"/>.</exception>
/// <param name="methodName">The name of the method or property to be invoked.</param>
/// <param name="exactParameterMatch">Should the parameter types match exactly types that were provided</param>
/// <param name="args">The optional arguments for the invocation. If argument matchers are used,
/// remember to use <see cref="ItExpr"/> rather than <see cref="It"/>.</param>
/// <param name="times">The number of times a method is allowed to be called.</param>
/// <typeparam name="TResult">The type of return value from the expression.</typeparam>
[SuppressMessage("Microsoft.Design", "CA1004:GenericMethodsShouldProvideTypeParameter")]
void Verify<TResult>(string methodName, Times times, bool exactParameterMatch, params object[] args);

/// <summary>
/// Specifies a verify for an invocation on a property getter with the given
/// <paramref name="propertyName"/>.
Expand Down
14 changes: 12 additions & 2 deletions src/Moq/Protected/ProtectedMock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,27 @@ public ISetupSequentialResult<TResult> SetupSequence<TResult>(string methodOrPro
#region Verify

public void Verify(string methodName, Times times, object[] args)
{
this.Verify(methodName, times, false, args);
}

public void Verify(string methodName, Times times, bool exactParameterMatch, object[] args)
{
Guard.NotNullOrEmpty(methodName, nameof(methodName));

var method = GetMethod(methodName, args);
var method = GetMethod(methodName, exactParameterMatch, args);
ThrowIfMemberMissing(methodName, method);
ThrowIfPublicMethod(method, typeof(T).Name);

Mock.VerifyVoid(mock, GetMethodCall(method, args), times, null);
}

public void Verify<TResult>(string methodName, Times times, object[] args)
{
this.Verify<TResult>(methodName, times, false, args);
}

public void Verify<TResult>(string methodName, Times times, bool exactParameterMatch, object[] args)
{
Guard.NotNullOrEmpty(methodName, nameof(methodName));

Expand All @@ -178,7 +188,7 @@ public void Verify<TResult>(string methodName, Times times, object[] args)
return;
}

var method = GetMethod(methodName, args);
var method = GetMethod(methodName, exactParameterMatch, args);
ThrowIfMemberMissing(methodName, method);
ThrowIfPublicMethod(method, typeof(T).Name);

Expand Down
37 changes: 37 additions & 0 deletions tests/Moq.Tests/VerifyFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Threading.Tasks;

using Moq;
using Moq.Protected;
Comment thread
Shereef marked this conversation as resolved.
Resolved

using Xunit;

Expand Down Expand Up @@ -1516,6 +1517,17 @@ public void VerifyAll_ignores_setups_from_SetupAllProperties_but_not_other_prope
mock.VerifyAll();
}

[Fact]
public void VerifyProtectedMethodOnChildClass()
{
var mock = new Mock<Child>();
mock.Protected().Setup("Populate", exactParameterMatch: true, ItExpr.Ref<ChildDto>.IsAny).CallBase().Verifiable();
ChildDto dto = new ChildDto();
_ = mock.Object.InvokePopulate(ref dto);

mock.Protected().Verify("Populate", Times.Once(), exactParameterMatch: true, ItExpr.Ref<ChildDto>.IsAny);
}

public interface IBar
{
int? Value { get; set; }
Expand Down Expand Up @@ -1570,6 +1582,31 @@ public interface ICat
string Purr(int amount);
void Hiss();
}

public class ParentDto { }
Comment thread
Shereef marked this conversation as resolved.
Resolved

public class ChildDto : ParentDto { }

public class Parent
{
protected virtual bool Populate(ref ParentDto dto)
{
return true;
}
}

public class Child : Parent
{
protected virtual bool Populate(ref ChildDto dto)
{
return true;
}

public bool InvokePopulate(ref ChildDto dto)
{
return Populate(ref dto);
}
}
}
}

Expand Down
0