3 回答
TA贡献1864条经验 获得超2个赞
虽然问题是关于Moq 3(可能是由于其年龄),但请允许我发布Moq 4.8的解决方案,该解决方案对by-ref参数的支持有了很大改进。
public interface IGobbler
{
bool Gobble(ref int amount);
}
delegate void GobbleCallback(ref int amount); // needed for Callback
delegate bool GobbleReturns(ref int amount); // needed for Returns
var mock = new Mock<IGobbler>();
mock.Setup(m => m.Gobble(ref It.Ref<int>.IsAny)) // match any value passed by-ref
.Callback(new GobbleCallback((ref int amount) =>
{
if (amount > 0)
{
Console.WriteLine("Gobbling...");
amount -= 1;
}
}))
.Returns(new GobbleReturns((ref int amount) => amount > 0));
int a = 5;
bool gobbleSomeMore = true;
while (gobbleSomeMore)
{
gobbleSomeMore = mock.Object.Gobble(ref a);
}
顺便说一句:It.Ref<T>.IsAny也适用于C#7 in参数(因为它们也是by-ref)。
- 3 回答
- 0 关注
- 487 浏览
添加回答
举报