Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 800
baynezy Avatar asked Feb 02 '13 14:02

baynezy


1 Answers

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.

like image 190
Jeppe Stig Nielsen Avatar answered Sep 22 '22 06:09

Jeppe Stig Nielsen