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

Capture.In during Verify stopped working when upgrading Moq from 4.10.1 to 4.13.1 #968

Closed
vgriph opened this issue Jan 13, 2020 · 2 comments · Fixed by #974
Closed

Capture.In during Verify stopped working when upgrading Moq from 4.10.1 to 4.13.1 #968

vgriph opened this issue Jan 13, 2020 · 2 comments · Fixed by #974
Labels
Milestone

Comments

@vgriph
Copy link

vgriph commented Jan 13, 2020

With Moq 4.10.1 we used to be able to do the following to capture a NaturalIdentifier NHibernate criterion added to an NHibernate criteria:

Mock<ICriteria> criteriaMock
var naturalIds = new List<NaturalIdentifier>();
criteriaMock.Verify(x => x.Add(Capture.In(naturalIds)));
var naturalId = naturalIds.Single();

After upgarding to 4.13.1, the naturalIds list is empty after passing the Verify call, resuting in an exception from the Single call.

As a workaround we've had to change our test to instead work on the Invocations collection:

var addInvocation = criteriaMock.Invocations
    .Single(x => x.Method.Name == nameof(ICriteria.Add));
var naturalId = (NaturalIdentifier)addInvocation.Arguments[0];

The workaround work, but feels less type safe since we have to match the method cal by name rather than by the expression tree (or do our own expression three handling to get the MethodInfo from an expression.)

@stakx
Copy link
Contributor

stakx commented Mar 2, 2020

It appears that Moq's behavior regarding this changed as a consequence of #844 (more specifically, 84324d1). /cc @ocoanet

However, I believe that the new behavior is correct, since Verify is not supposed to cause any side effects. Capture.In is meant to be used in conjunction with Setup:

var criteriaMock = new Mock<ICriteria>();
var naturalIds = new List<NaturalIdentifier>();
criteriaMock.Setup(x => x.Add(Capture.In(naturalIds)));

... // exercise the mock

var naturalId = naturalIds.Single();

I suggest you change your tests accordingly, it seems unlikely to me that we'll go back to the old behavior.

Let me know if you have any questions. If not, I'll close this issue as "won't fix" in a few days' time.

@stakx
Copy link
Contributor

stakx commented Mar 8, 2020

I took a closer look, and this turned out fairly easy to fix. While Capture.In perhaps wasn't meant to be used with Verify at all, fact is that it did work for some time. I don't see any direct harm it getting it working again. Just don't tell anyone this is a thing that works (i.e. please don't put it in the wiki) 😄

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

Successfully merging a pull request may close this issue.

2 participants