有没有办法使模拟函数与ON_CALL "interesting"?
Is there a way to make mock functions "interesting" with ON_CALL?
给定:
#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
指定时,mock函数调用才有意思吗?
"只有在使用EXPECT_CALL指定时,mock函数调用才有趣吗?"
简而言之,是的ON_CALL()
宏只影响要对mock方法调用执行的操作,而不影响对mock对象设置的调用期望值。
不过,您可以使用NiceMock<>
模板来抑制这些警告
引用谷歌模拟"食谱"
假设您的测试使用模拟类MockFoo:
TEST(...) {
MockFoo mock_foo;
EXPECT_CALL(mock_foo, DoThis());
... code that uses mock_foo ...
}
如果调用了DoThis()以外的mock_foo方法,Google mock会将其报告为警告。然而,如果您重写测试以使用NiceMock,则警告将消失,从而产生更干净的测试输出:
using ::testing::NiceMock;
TEST(...) {
NiceMock<MockFoo> mock_foo;
EXPECT_CALL(mock_foo, DoThis());
... code that uses mock_foo ...
}
NiceMock<MockFoo>
是MockFoo
的一个子类,因此它可以在接受MockFoo
的任何地方使用。
可以将EXPECT_CALL与WillRepeated一起使用:
EXPECT_CALL(mock, DoThis()).WillRepeatedly(Return("42"));
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 学习 c++,编译错误"no matching function for call"
- 结构错误:"no matching function for call to "
- 创建函数变体向量时"No matching function for call"错误
- C++线程"Call to non-static member function without an object argument"
- 编译时二叉搜索树错误的反向迭代器表示"no matching function call for operator=()"
- 如果子类中没有构造函数方法,则错误"no matching function for call to 'LGame::LGame(String&)'"
- 提升多精度浮点数128:标准::exp错误:'no matching function for call'
- "error: no matching function for call to",我无法更改结构的定义
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- 替换派生对象向量中的对象"no matching function to call"
- ROS CPP 相当于 Python subprocess.call() 和 shell=True
- (C++)构造函数、默认参数"call of overloaded... ambigous"
- 我的程序显示许多错误,"error: no matching function for call to 'getPercentScore'"