c# - 使用 Typemock 断言一个方法被调用了 x 次

标签 c# unit-testing typemock typemock-isolator

我有一个带有这个签名的方法:

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

我想验证此方法在 x = 5y = 10 时被调用了 2 次。我如何使用 Typemock 做到这一点?

最佳答案

我试了一下,得出了以下结论:

给定类:

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

您的测试将如下所示:

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

关于c# - 使用 Typemock 断言一个方法被调用了 x 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47156499/

相关文章:

unit-testing - SBT 在 Karma 测试失败时退出

c# - NUnit 和 Typemock 的奇怪问题

c# - 如何验证typemock中私有(private)方法调用的数量

c# - Mocking 是否能够替换包装在方法中的功能?

c# - 将 List<string> 绑定(bind)到 asp.net 中的 Grid View 的一列

c# - 如何使用 Emgu c# 加载 CascadeClassifier

c# - ASP.NET MVC 或 ASP.NET Web API + AngularJS

c# List<string> 赋值问题

java - 验证是否使用 mockito 调用了三种方法之一

java - 从单元测试启动和停止 hsqldb