mock.Protected().Verify() does not work correctly #1493
Closed
Description
Describe the Bug
mock.Protected().Verify(...)
does not work correctly when used with wildcard matchers such as It.IsAny
.
Steps to Reproduce
using Moq;
using Moq.Protected;
namespace TestProject1;
public class UnitTest1
{
[Fact]
public void Verify_that_the_Public_method_is_called_with_the_specific_value_for_the_argument()
{
var mock = new Mock<X>();
mock.Object.CallPublic(1);
mock.Verify(static x => x.Public(1), Times.Once());
}
[Fact]
public void Verify_that_the_Public_method_is_called_regardless_of_the_value_of_its_argument()
{
var mock = new Mock<X>();
mock.Object.CallPublic(2);
mock.Verify(static x => x.Public(It.IsAny<int>()), Times.Once());
}
[Fact]
public void Verify_that_the_Protected_method_is_called_with_the_specific_value_for_the_argument()
{
var mock = new Mock<X>();
mock.Object.CallProtected(3);
mock.Protected().Verify("Protected", Times.Once(), 3);
}
[Fact]
public void Verify_that_the_Protected_method_is_called_regardless_of_the_value_of_its_argument()
{
var mock = new Mock<X>();
mock.Object.CallProtected(4);
mock.Protected().Verify("Protected", Times.Once(), It.IsAny<int>());
}
}
public abstract class X
{
public void CallPublic(int value) => Public(value);
public void CallProtected(int value) => Protected(value);
public abstract void Public(int value);
protected abstract void Protected(int value);
}
Expected Behavior
All tests passed.
Exception with Stack Trace
Moq.MockException
Expected invocation on the mock once, but was 0 times: mock => mock.Protected(0)
Performed invocations:
Mock<X:3> (mock):
X.Protected(4)
at Moq.Mock.Verify(Mock mock, LambdaExpression expression, Times times, String failMessage) in /_/src/Moq/Mock.cs:line 331
at Moq.Protected.ProtectedMock`1.InternalVerify(String methodName, Type[] genericTypeArguments, Times times, Boolean exactParameterMatch, Object[] args) in /_/src/Moq/Protected/ProtectedMock.cs:line 234
at Moq.Protected.ProtectedMock`1.Verify(String methodName, Times times, Object[] args) in /_/src/Moq/Protected/ProtectedMock.cs:line 206
at TestProject1.UnitTest1.Verify_that_the_Protected_method_is_called_regardless_of_the_value_of_its_argument() in D:\Source\TestProject1\UnitTest1.cs:line 37
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)
Version Info
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<IsTestProject>true</IsTestProject>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="coverlet.collector" Version="6.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.10.0" />
<PackageReference Include="xunit" Version="2.9.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Moq" Version="4.20.70" />
</ItemGroup>
<ItemGroup>
<Using Include="Xunit" />
</ItemGroup>
</Project>