How to make fun of a method that returns void but changes the type of link passed using Moq

I have an Interface

 public interface IRequester { void Check(Check check); } 

I want to mock this with Moq , which is obviously easy. The problem is that I want the passed Check be changed (as a link) after the mocking call. As you can see, Check is just 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 passed Check .

Is it possible?

+7
source share
2 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 curly braces { } and the first semicolon ; if you need only one appointment.

+15
source

I think you can use the ref keyword to get the right thing:

The ref parameter parameter keyword in the method parameter calls the method to refer to the same variable that was passed to this method. Any changes made to a parameter in a method will be reflected in that variable when control passes to the calling method.

http://msdn.microsoft.com/en-us/library/14akc2c7(v=vs.71).aspx

0
source

All Articles