能够将数组传递给模拟方法调用
我有一个我想模拟的方法,它接受一个数组作为参数。在实际调用中,该方法将修改此数组,并且生成的数组将在代码中进一步使用。 我尝试做的是将数组传递给模拟方法调用,该方法调用的值在再次使用数组时有效。然而我发现当调用模拟方法时,它不使用我在设置模拟时指定的数组,而是使用原始数组,有没有办法解决这个问题。
例如,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您在这里所做的唯一一件事就是设置一个期望,即您的 Do 方法将使用 returnArray 作为参数来调用,并且它将返回 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
using Rhino Mock this would look like this (using moq it will be equivalent):