【问题标题】:Mock object name is missing in warning警告中缺少模拟对象名称
【发布时间】:2014-10-09 08:30:40
【问题描述】:

当 gmock 看到一个它不期望的方法调用时,它会写一个这样的警告:

GMOCK WARNING:
Uninteresting mock function call - returning directly.
    Function call: Constructor()
Stack trace:

当单元测试中的每个模拟对象都有一个名为“构造函数”的方法时,这并不是很有帮助,因为找出哪个对象创建了这条消息并且缺少一个 EXPECT_CALL 并不总是那么容易。

有没有办法告诉 gmock 在这样的警告中也写类名或模拟对象的名称?

【问题讨论】:

    标签: unit-testing gmock


    【解决方案1】:

    我们已经针对这个问题实施了另一种解决方案。我们有自己的 Eclipse 插件,它从选定的头文件生成模拟对象文件。现在我们更改插件以生成一个包含类名的模拟名称,例如:

      MOCK_METHOD0(Timer_Constructor, void());
    

    这会导致警告

    Uninteresting mock function call - returning directly.
    Function call: Timer_Constructor()
    

    【讨论】:

      【解决方案2】:

      这真的很不方便。

      查看the source of the uninteresting call function report,似乎无法修改此行为。 a fixed enum 给出了对方法调用的可能反应,因此扩展在这里看起来不像是一个选项。您可以将 Google Test 事件侦听器附加到测试套件,但我认为到达这些侦听器的信息同样有限。

      在我看来,如果这对我来说真的很重要,我会修改 Google Mock 源代码中提到的行,并连同方法名称一起放入对象地址(这是最接近和标识符)与MockObject()。比如:

        // Writes a message that the call is uninteresting (i.e. neither
        // explicitly expected nor explicitly unexpected) to the given
        // ostream.
        virtual void UntypedDescribeUninterestingCall(
            const void* untyped_args,
            ::std::ostream* os) const
                GTEST_LOCK_EXCLUDED_(g_gmock_mutex) {
          const ArgumentTuple& args =
              *static_cast<const ArgumentTuple*>(untyped_args);
          *os << "Uninteresting mock function call - ";
          DescribeDefaultActionTo(args, os);
          *os << "      Function call: " << Name();
          *os << "Mock object address: " << MockObject();
          UniversalPrint(args, os);
        }
      

      这并不像看起来那么疯狂; Google Mock 是一个测试库,而不是生产库,因此自定义(受控)修改在那里并没有那么有害。实际上Google does recommend use a custom compilation of Google Test for each different project

      或者你可以把补丁发给他们看看他们是否喜欢:)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-10-26
        • 2014-11-06
        • 1970-01-01
        • 2018-08-17
        • 2018-03-19
        • 2013-05-16
        • 2015-11-20
        • 1970-01-01
        相关资源
        最近更新 更多