Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asserting that a method was called x times with exact arguments using Typemock

I have a method with this signature:

public static void foo(int x, int y)
{
    //do something...
}

I want to verify that this method was called exactly 2 times when x = 5 and y = 10. How can I do that using Typemock?

like image 800
HilaB Avatar asked Nov 17 '25 22:11

HilaB


1 Answers

I gave this a go and came up with the following:

Given the class:

public class Bar
{
    public static void Foo(int x, int y)
    {
        //do something...
        Debug.WriteLine($"Method called with {x} {y}");
    }
}

Your test would then look like this:

[TestClass]
public class Test
{
    [TestMethod]
    public void TestMethod()
    {
        var callCount = 0;

        Isolate.WhenCalled(() => Bar.Foo(2, 10))
            .WithExactArguments()
            .DoInstead(context =>
            {
                callCount++;
                context.WillCallOriginal();
            });

        Bar.Foo(2, 6);
        Bar.Foo(2, 10);
        Bar.Foo(2, 10);

        Assert.AreEqual(2, callCount);
    }
}
like image 98
TheRock Avatar answered Nov 19 '25 11:11

TheRock



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!