能够将数组传递给模拟方法调用

发布于 2024-08-22 04:57:03 字数 815 浏览 7 评论 0原文

我有一个我想模拟的方法,它接受一个数组作为参数。在实际调用中,该方法将修改此数组,并且生成的数组将在代码中进一步使用。 我尝试做的是将数组传递给模拟方法调用,该方法调用的值在再次使用数组时有效。然而我发现当调用模拟方法时,它不使用我在设置模拟时指定的数组,而是使用原始数组,有没有办法解决这个问题。

例如,

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) 

这就是我的测试失败的地方,因为数组仍然是上面两行声明的空数组,而不是我在模拟方法调用中指定的数组。希望这能解释我想要实现的目标,如果是的话,无论如何都可以做到这一点。如果可以的话,也愿意使用 RhinoMocks。

预先感谢,

凯夫

I have a method that i want to mock that takes an array as a argument. In a real call, the method would modify this array and the resultant array would be use further along in code.
What i tried to do was pass in array to the mocked method call, that had values that would be valid when the array is used again. However what i find is when the call is being made to the mocked method it doesn't use the array i have specfied when setting up the mock, instead using the original array, Is there a way around this problem.

e.g

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) 

This is where my test falls down as array is still the null array declared two lines above and not the array i specified in the mocked method call. Hope this explains what i am trying to achieve and if so is there anyway of doing it. Would also be open to using RhinoMocks as well if it could be done that way.

Thank in advance,

Kev

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱纠缠 2024-08-29 04:57:03

您在这里所做的唯一一件事就是设置一个期望,即您的 Do 方法将使用 returnArray 作为参数来调用,并且它将返回 0 作为结果。您想要做的是

  1. 创建一个严格的模拟,以更好地查看正在执行的内容,
  2. 设置一个需要初始数组的调用,然后运行委托以使用 Rhino Mock 将值设置为 1, 2, 3,

这看起来像这样(使用起订量将是等效的):

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]);
}

The only thing you have done here is to set up an expectation that your Do method will be called with returnArray as parameter, and that it will return 0 as the result. What you want to do is to

  1. create a strict mock to better see what is being executed
  2. setup a call that expects your initial array, and then runs a delegate to set the values to 1, 2, 3

using Rhino Mock this would look like this (using moq it will be equivalent):

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]);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文