简体   繁体   English

谷歌测试期望从函数调用

[英]Google Test Expect Call from a Function

Say I have a simple test with mocks.假设我有一个简单的模拟测试。

#include "boost/interprocess/detail/interprocess_tester.hpp"
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace ::testing;

struct IInterface
{
    virtual void method(int foo) = 0;
};

struct SimpleMock : public IInterface
{
    MOCK_METHOD1(method, void(int foo));
};

struct TestFixture : public Test
{
    TestFixture()
    {
        // Don't care about other invocations not expected
        EXPECT_CALL(mock, method(_)).Times(AnyNumber());
    }

    void setupExpectation(int data)
    {
        EXPECT_CALL(mock, method(data)).Times(1);
    }

    SimpleMock mock;
};

TEST_F(TestFixture, SimpleTest)
{
    setupExpectation(2);

    mock.method(2);

    setupExpectation(5);

    mock.method(3); // will fail expectation
}

This will fail with the message below referencing into the helper method, which makes it hard to debug or figure which expectation failed since I don't see the line I called setupExpectation or the actual argument value.这将失败,下面的消息引用辅助方法,这使得很难调试或确定哪个期望失败,因为我没有看到我调用setupExpectation的行或实际参数值。

test_HarmonicTherapyStateMachineAit.cpp:27: Failure
Actual function call count doesn't match EXPECT_CALL(mock, method(data))...
         Expected: to be called once
           Actual: never called - unsatisfied and active

Note my actual use case has more complicated expect calls where I think it warrants splitting it into a separate method (and having multiple expectations in one test).请注意,我的实际用例有更复杂的期望调用,我认为有必要将其拆分为一个单独的方法(并且在一个测试中具有多个期望)。 However, I'm not sure how to get a more informative error message from this.但是,我不确定如何从中获得更多信息丰富的错误消息。

I've read about http://google.github.io/googletest/gmock_cook_book.html#controlling-how-much-information-gmock-prints .我读过http://google.github.io/googletest/gmock_cook_book.html#controlling-how-much-information-gmock-prints However, this provides more information than I really want, which is just a line number of the function that calls setupExpectation .然而,这提供了比我真正想要的更多的信息,这只是调用setupExpectation的函数的行号。

I also just tried making a MACRO to wrap the common expectations.我也只是尝试制作一个宏来包装共同的期望。 This would be easy in this simple case.在这种简单的情况下,这很容易。 However, my actual use case has more complicated logic that I'd rather not place into a macro.但是,我的实际用例具有更复杂的逻辑,我不想将其放入宏中。

Even if I could do something like EXPECT_CALL(...).Times(1) << "argument: " << foo;即使我可以做类似EXPECT_CALL(...).Times(1) << "argument: " << foo; That would be helpful.那会很有帮助。

Any help would be appreciated.任何帮助,将不胜感激。

Currently I had the same problem, that gmock provides me very few information.目前我遇到了同样的问题,gmock 为我提供的信息很少。 My solution was to write a custom Matcher.我的解决方案是编写一个自定义匹配器。

The syntax of the EXPECT_CALL macro is according to the documentation : EXPECT_CALL 宏的语法根据文档

EXPECT_CALL(mock_object, method(matchers))

A quick solution could be to write a matcher , which in case of an error prints a message to the screen.一个快速的解决方案可能是编写一个matcher ,如果出现错误,它会在屏幕上打印一条消息。

eg:例如:

MATCHER_P2(isEqual, expected, line, "")
{
  bool isEqual = arg == expected;

  if(!isEqual)
    printf("Expected argument: %d, actual: %d (EXPECT_CALL line: %d)\n", expected, arg, line);

  return isEqual;
}

The EXPECT_CALL then changes to EXPECT_CALL 然后更改为

EXPECT_CALL(mock, method(isEqual(3, __LINE__))).Times(1);

Please let me know if you have another/better solution to this.如果您有其他/更好的解决方案,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM