Search code examples
c#unit-testingtypemocktypemock-isolator

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?


Solution

  • 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);
        }
    }