C# 使用NSubstitute检查收到的呼叫数是否在范围内
是否有办法与NSUSITUTE核实收到的呼叫数是否在一定范围内 我想做一些类似的事情:C# 使用NSubstitute检查收到的呼叫数是否在范围内,c#,mocking,nsubstitute,C#,Mocking,Nsubstitute,是否有办法与NSUSITUTE核实收到的呼叫数是否在一定范围内 我想做一些类似的事情: myMock.Received(r => r > 1 && r <= 5).MyMethod(); myMock.Received(r=>r>1&&rnsubstituteapi目前并不完全支持这一点(但这是一个好主意!) 使用.ReceivedCalls扩展名有一种不太成熟的方法: var calls = myMock.ReceivedCalls() .Count
myMock.Received(r => r > 1 && r <= 5).MyMethod();
myMock.Received(r=>r>1&&rnsubstituteapi目前并不完全支持这一点(但这是一个好主意!)
使用.ReceivedCalls
扩展名有一种不太成熟的方法:
var calls = myMock.ReceivedCalls()
.Count(x => x.GetMethodInfo().Name == nameof(myMock.MyMethod));
Assert.InRange(calls, 1, 5);
使用NSubstitute.ReceivedExtensions
命名空间中的自定义Quantity
执行此操作的更好方法:
// DISCLAIMER: draft code only. Review and test before using.
public class RangeQuantity : Quantity {
private readonly int min;
private readonly int maxInclusive;
public RangeQuantity(int min, int maxInclusive) {
// TODO: validate args, min < maxInclusive.
this.min = min;
this.maxInclusive = maxInclusive;
}
public override string Describe(string singularNoun, string pluralNoun) =>
$"between {min} and {maxInclusive} (inclusive) {((maxInclusive == 1) ? singularNoun : pluralNoun)}";
public override bool Matches<T>(IEnumerable<T> items) {
var count = items.Count();
return count >= min && count <= maxInclusive;
}
public override bool RequiresMoreThan<T>(IEnumerable<T> items) => items.Count() < min;
}
(注意,您需要使用NSubstitute.ReceivedExtensions;
来完成此操作。)我已经(链接到这里并向您推荐:)使用NUnit 3,我必须执行Assert。因为我找不到Assert.InRange.Assert.InRange是来自XUnit的。
myMock.Received(new RangeQuantity(3,5)).MyMethod();