0

I have a callback method called filter that take in a function and a single string parameter. In my code, filter is called two different times with different methods and parameters. Is there a way to set the parameter different for each instance filter is called?

private void method1 {
    filter(iq1, function1);    
}

private void method2 {
    filter(iq2, function2);
}

This is how tried to set up the mock

private Mock<Filter> m = new Mock<Filter>();
string test1 = "hello";
string test2 = "goodbye";

var queue = new Queue<string>();
queue.Enqueue(test1);
queue.Enqueue(test2);

m.Setup(f => f.filter(It.IsNotNull<string>, It.IsAny<Action<Filter>>).Callback((string iq, Action<Filter> action) => action(queue.Dequeue());

I know that method1 calls filter before method2.

ghost013
  • 97
  • 1
  • 10
  • 2
    You are already using `It.IsAny`. Are you aware of `It.Is(s => s.Equals("stringValue")`? That way you can have as many setups as you need. – Dan Jul 01 '14 at 20:21
  • Dan, I'm afraid I don't understand. I am new to moq, so I don't understand how this works. I need a way to ensure that test1 is used a parameter in the filter call in method1 and test2 is used in method2. – ghost013 Jul 02 '14 at 13:06
  • This might help, viz where you need to return different values for different inputs : http://stackoverflow.com/a/24198206/314291. You can also verify that the Mock was called with the expected values in the correct order with `Sequences`: http://stackoverflow.com/a/24029959/314291 – StuartLC Jul 02 '14 at 14:20

0 Answers0