如何使用 FakeItEasy 检查是否一定发生了对任何重载的调用?

发布于 2024-10-21 15:11:52 字数 526 浏览 6 评论 0原文

我有一个正在测试的方法。给定某些输入,它应该向记录器(ILogger)写入失败方法。该接口有几个 Log() 重载,以及一些属性(例如日志记录级别)。我正在使用 FakeItEasy 嘲笑记录器。

我想断言的是对 Log() 的调用已经发生。但是,我不关心使用了哪个特定的重载。我该怎么做?

我的想法:

// Doesn't work, since a different overload (with more parameters) is used.
A.CallTo(() => mockLogger.Log(null)).WithAnyArguments().MustHaveHappened();

// "Works", but if the code were to call something else on the logger
// (ex. change the logging level), this would also pass!
Any.CallTo(mockLogger).MustHaveHappened();

I have a method which I am testing. Given certain inputs, it should write a failure method to the logger (an ILogger). The interface has several overloads for Log(), as well as some properties (ex. a logging level). I am mocking the logger using FakeItEasy.

What I want to assert is that a call to Log() has happened. However, I don't care about which specific overload got used. How can I do this?

My ideas:

// Doesn't work, since a different overload (with more parameters) is used.
A.CallTo(() => mockLogger.Log(null)).WithAnyArguments().MustHaveHappened();

// "Works", but if the code were to call something else on the logger
// (ex. change the logging level), this would also pass!
Any.CallTo(mockLogger).MustHaveHappened();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

厌味 2024-10-28 15:11:52

编辑

这可以使用以下语法来完成:

A.CallTo(logger).Where(x => x.Method.Name == "Log").MustHaveHappened()

原始答案

没有简单的方法可以做到这一点,并且 - 如果我错了,请纠正我 - 我认为没有在任何模拟框架中。

话虽如此,下面的示例中提供了一种不太简单的方法来做到这一点。

public interface ILogger
{
    void Log(string value);
    void Log(object value);
}

public class LoggerTests
{
    public void FakeGetCallsExample()
    {
        var logger = A.Fake<ILogger>();

        logger.Log("whatever");

        var callsToLog = Fake.GetCalls(logger).Where(x => x.Method.Name.Equals("Log"));

        // Asserting with NUnit.
        Assert.That(callsToLog(), Is.Not.Empty);
    }

    // The following does not work as of now but I'll seriously consider
    // implementing it:
    public void AnyCallToWithCallSpecificationExample()
    {
        var logger = A.Fake<ILogger>();

        logger.Log("whatever");

        // I would add a "filtering" method to the Any.CallTo-syntax:
        Any.CallTo(logger).WhereCallMatches(x => x.Method.Name.Equals("Log")).MustHaveHappened();

        // It would also enable an extension method:
        Any.CallTo(logger).ToMethodNamed("Log").MustHaveHappened();
    }
}

Edit

This can be done using the following syntax:

A.CallTo(logger).Where(x => x.Method.Name == "Log").MustHaveHappened()

Original answer

There's no easy way of doing that, and - correct me if I'm wrong - I don't think there is in any mocking framework.

That being said there is a - not so easy - way to do it provided in the example below.

public interface ILogger
{
    void Log(string value);
    void Log(object value);
}

public class LoggerTests
{
    public void FakeGetCallsExample()
    {
        var logger = A.Fake<ILogger>();

        logger.Log("whatever");

        var callsToLog = Fake.GetCalls(logger).Where(x => x.Method.Name.Equals("Log"));

        // Asserting with NUnit.
        Assert.That(callsToLog(), Is.Not.Empty);
    }

    // The following does not work as of now but I'll seriously consider
    // implementing it:
    public void AnyCallToWithCallSpecificationExample()
    {
        var logger = A.Fake<ILogger>();

        logger.Log("whatever");

        // I would add a "filtering" method to the Any.CallTo-syntax:
        Any.CallTo(logger).WhereCallMatches(x => x.Method.Name.Equals("Log")).MustHaveHappened();

        // It would also enable an extension method:
        Any.CallTo(logger).ToMethodNamed("Log").MustHaveHappened();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文