如何检查 NSubstitute 模拟中使用的参数

How to check the parameters that were used in an NSubstitute mock

我知道如何断言对模拟接口上的方法的调用是使用特定参数或任何参数完成的。

但是,我想存储用于调用函数的参数和 运行 另一个断言,但我找不到任何方法可以做到这一点。

如何存储在调用模拟方法时使用的参数值?

您可以通过 Arg.Do 捕获参数,如 Performing actions with arguments:

中所述
var firstArgsBeingMultiplied = new List<int>(); 
calculator.Multiply(Arg.Do<int>(x => firstArgsBeingMultiplied.Add(x)), 10);

calculator.Multiply(2, 10);
calculator.Multiply(5, 10);
calculator.Multiply(7, 4567); //Will not match our Arg.Do as second arg is not 10

Assert.AreEqual(firstArgsBeingMultiplied, new[] { 2, 5 });

也可以从 .Returns or When..Do 回调中访问和存储参数。

希望这对您有所帮助。