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

mock.Protected().Verify() does not work correctly #1493

Closed
aetos382 opened this issue Jul 23, 2024 · 2 comments
Closed

mock.Protected().Verify() does not work correctly #1493

aetos382 opened this issue Jul 23, 2024 · 2 comments
Labels

Comments

@aetos382
Copy link

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>

Additional Info

@aetos382 aetos382 added the bug label Jul 23, 2024
@annina-k
Copy link

Since you are setting up IProtectedMock will using Moq.Protected.ItExpr instead of Moq.It fix your issue? https://github.com/devlooped/moq/blob/main/src/Moq/Protected/ItExpr.cs

@aetos382
Copy link
Author

Yes. That solved it. Thank you so much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants