c++ - 有没有办法用 ON_CALL 制作模拟函数 "interesting"?

标签 c++ unit-testing googlemock

给定:

#include "gmock/gmock.h"
#include <string>

using namespace testing; // tsk, tsk

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

struct Mockable {
    virtual std::string ify(int x) const;
};

std::string Mockable::ify(int x) const
{
    return std::to_string(x);
}

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

struct Mocked : public Mockable {
    MOCK_CONST_METHOD1(ify, std::string(int));
};

// -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

std::string tested(Mockable const& u, int x)
{
    return u.ify(x);
}

TEST(TC,T42)
{
    Mocked mock;
    ON_CALL(mock, ify(Eq(42)))
    .WillByDefault(Return("33"));

    std::string const& ret = tested(mock, 42);
    EXPECT_EQ("42", ret);
}

TEST(TC,T33)
{
    Mocked mock;
    ON_CALL(mock, ify(Eq(33)))
    .WillByDefault(Return("333"));

    std::string const& ret = tested(mock, 42);
    EXPECT_EQ("42", ret);
}

int main(int argc, char *argv[])
{
    ::testing::InitGoogleTest(&argc, argv);
    //::testing::FLAGS_gmock_verbose = "info";

    return RUN_ALL_TESTS();
}

输出是:

$ ./mocktest 
[==========] Running 2 tests from 1 test case.
[----------] Global test environment set-up.
[----------] 2 tests from TC
[ RUN      ] TC.T42

GMOCK WARNING:
Uninteresting mock function call - taking default action specified at:
mocktest.cc:40:
    Function call: ify(42)
          Returns: "33"
Stack trace:
mocktest.cc:44: Failure
Value of: ret
  Actual: "33"
Expected: "42"
[  FAILED  ] TC.T42 (0 ms)
[ RUN      ] TC.T33

GMOCK WARNING:
Uninteresting mock function call - returning default value.
    Function call: ify(42)
          Returns: ""
Stack trace:
mocktest.cc:54: Failure
Value of: ret
  Actual: ""
Expected: "42"
[  FAILED  ] TC.T33 (1 ms)
[----------] 2 tests from TC (1 ms total)

[----------] Global test environment tear-down
[==========] 2 tests from 1 test case ran. (1 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 2 tests, listed below:
[  FAILED  ] TC.T42
[  FAILED  ] TC.T33

在这两种情况下,Gmock 的行为都是正确的。应用行为(或不应用,在 TC33 中)。但是为什么在这两种情况下都说“无趣的模拟函数调用”呢?模拟函数调用是否只有在使用 EXPECT_CALL 指定时才有意义?

最佳答案

"Are mock function calls interesting only when specified with EXPECT_CALL ?"

简而言之,是的。
ON_CALL()宏仅影响要对模拟方法调用执行的操作,而不影响在模拟对象上设置的调用预期。

您可以使用 NiceMock<>模板,但一般会抑制这些警告。
To cite from google mocks "Cookbook"

Suppose your test uses a mock class MockFoo:

TEST(...) {
  MockFoo mock_foo;
  EXPECT_CALL(mock_foo, DoThis());
  ... code that uses mock_foo ...
}

If a method of mock_foo other than DoThis() is called, it will be reported by Google Mock as a warning. However, if you rewrite your test to use NiceMock instead, the warning will be gone, resulting in a cleaner test output:

using ::testing::NiceMock;

TEST(...) {
  NiceMock<MockFoo> mock_foo;
  EXPECT_CALL(mock_foo, DoThis());
  ... code that uses mock_foo ...
}

NiceMock<MockFoo> is a subclass of MockFoo, so it can be used wherever MockFoo is accepted.

关于c++ - 有没有办法用 ON_CALL 制作模拟函数 "interesting"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26156879/

相关文章:

c++ - 无法从 'unsigned int' 转换为 'unsigned int&'

c++ - 如何使这些测试用例代码美观?

c++ - GMock,调用 SaveArg 捕获的 std::function

c++ - Google Mock 中的双重免费

c++ - 在谷歌测试框架中,如何期待一个函数调用或另一个函数调用?

c++ - QSignalMapper 和 QAction 的问题永远不会触发插槽

c++ - 编译器没有发现歧义

c++ - 在类 File 和 Buffer 中查找段错误

c# - Rhino Mocks - stub 一个单例

java - Java单元测试如何触发JsonProcessingException?