Ability to pass an array to a mock method call

I have a method that I want to make fun of that takes an array as an argument. In a real call, the method will modify this array, and the resulting array will be used later in the code. What I was trying to do was pass the array into a laughed method call that would have values ​​that would be valid when the array was used again. However, what I find is that when the call is made with the mocked method, it does not use the array that I defined when setting up the layout, instead using the original array. Is there any way around this problem.

eg

public interface ITest
{
    int Do(int[] values);
}

public void MyTest
{
    var testMock = new Mock<ITest>();
    int[] returnArray = new int[] {1,2,3};
    testMock.Setup(x => x.Do(returnArray)).Returns(0);
    MyTestObject obj = new MyTestObject();
    obj.TestFunction(testMock.Object);
}

public class MyTestObject
{
  .....
    public void TestFunction(ITest value)
    {
         int [] array = new int[3];
         value.Do(array);
         if (array[0] == 1) 

, - , , i, mocked method. , , , , . RhinoMocks, .

,

Kev

+3
1

, , - , Do returnArray , 0 . ,

  • , ,
  • , , , 1, 2, 3

Rhino Mock, ( moq, ):

private delegate int DoDelegate(int[] values);

[Test]
public void MyTest()
{
    var testMock = MockRepository.GenerateStrictMock<ITest>();
    testMock.Expect(x => x.Do(null))
        .IgnoreArguments()
        .Do(new DoDelegate(delegate(int[] values)
                             {
                                 values[0] = 1;
                                 values[1] = 2;
                                 values[2] = 3;
                                 return 0;
                             }));

    //Execution (use your real object here obviously
    int[] array = new int[3];
    testMock.Do(array);
    Assert.AreEqual(1, array[0]);
}
+1

Source: https://habr.com/ru/post/1733334/


All Articles