moq icon indicating copy to clipboard operation
moq copied to clipboard

Issue with multiple identical setups

Open koder-man opened this issue 2 years ago • 1 comments

Seen with 4.18.1.

Suppose we have this interface:

public interface ISample
{
    void Mock(object fi);
}

And the following code:

var mock = new Mock<ISample>();
mock.Setup(x => x.Execute(It.Is<object>(_ => true))).Verifiable();

var flag = true;
mock.Setup(x => x.Execute(It.Is<object>(_ => flag))).Verifiable();

mock.Object.Execute(new object());

mock.Verify();

Calling to mock.Verify():

  • Expected behaviour: call succeeds (behaviour in 4.18.0 and before).
  • Actual behaviour: call fails with error:
This mock failed verification due to the following:

MockSetupTests.ISample x => x.Execute(It.Is<object>(_ => True)):
This setup was not matched.

koder-man avatar Jan 27 '23 16:01 koder-man

Except those setups aren't truly identical. The question here is when captured variables should be resolved to their values: at the time when the setup is created vs. when an invocation happens that could match the setup.

There have been a few recent changes to this, so not sure we can go back to the previous behavior in your precise use case... but I'll look into it.

stakx avatar Apr 05 '23 12:04 stakx

Due to lack of recent activity, this issue has been labeled as 'stale'. It will be closed if no further activity occurs within 30 more days. Any new comment will remove the label.

github-actions[bot] avatar Aug 24 '24 20:08 github-actions[bot]

This issue will now be closed since it has been labeled 'stale' without activity for 30 days.

github-actions[bot] avatar Oct 04 '24 01:10 github-actions[bot]