How to mock a method that returns void but modifies the reference type passed in with Moq

c#, moq, unit-testing

Solution

Use the `Callback` method. I think it will be something like:

yourMock.Setup(x => x.Check(It.IsAny<Check>()))
    .Callback((Check c) => { c.Status = 1234567; });

You can leave out the braces `{ }` and the first semicolon `;` if you only need one assignment.

Problem

I have an `Interface` ``` public interface IRequester { void Check(Check check); } ``` I want to mock this using `Moq` which is obviously easy. The issue I have is that I want the `Check` passed in to be modified (as it is a reference) after the mocked call. As you can see `Check` is just a simple POCO. ``` public class Check { public string Url { get; set; } public int Status { get; set; } } ``` Ideally I want to change the value of the `Status` property on the `Check` passed in. Is this possible?

Original source