Moq版本4.8(或更高版本)对by-ref参数的支持大大改善:
public interface IGobbler{ bool Gobble(ref int amount);}delegate void GobbleCallback(ref int amount); // needed for Callbackdelegate bool GobbleReturns(ref int amount); // needed for Returnsvar 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);}相同的模式适用于
out参数。
It.Ref<T>.IsAny也适用于C#7
in参数(因为它们也是by-ref)。



