如何使用精确参数获取 GetTimesCalled?

How to GetTimesCalled with exact arguments?

我只是熟悉 Typemock Isolator,很抱歉,如果下面的问题很愚蠢。 我能以某种方式获得使用确切参数调用我的函数的时间吗? 喜欢 Isolate.Verify.GetTimesCalled() + Isolate.Verify.WasCalledWithExactArguments()

Typemock 没有获取精确参数调用次数的函数。但是,您可以使用 DoInstead():

解决此问题
public class UnderTestClass
{
    public void Foo(int n)
    {
        //Doesn't matter
    }
}

[TestMethod, Isolated]
public void VerifyNumberOfCalls()
{
    //Arrange
    var underTest = new UnderTestClass();

    int number = 0;
    Isolate.WhenCalled((int n) => underTest.Foo(n)).AndArgumentsMatch(n => n <= 0).DoInstead(context =>
    {
        number++;
        context.WillCallOriginal();
    });

    //Act
    underTest.Foo(2);
    underTest.Foo(1);
    underTest.Foo(0);
    underTest.Foo(-1);
    underTest.Foo(-2);

    //Assert
    Assert.AreEqual(3, number);
}