在EXPECT_CALL和Gmock之前使用ON_CALL时的奇怪行为
Odd Behavior When Using ON_CALL before EXPECT_CALL with Gmock
当ON_CALL语句后面跟着EXPECT_CALL语句时,有人见过gmock中的奇怪行为吗?对我来说,以下代码中的EXPECT_CALL语句不起作用(它实际上并没有强制执行Times部分(:
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock")));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
我尝试过的其他解决方案:
从超级类重写myMockMethod,并让它简单地返回一个字符串文本。这个问题是我无法确定它后来被调用了多少次
跳过ON_CALL部分,选择以下内容:
EXPECT_CALL(myMockObject, myMockMethod())
.Times(1)
.WillRepeatedly(Return("hello mock"));
这会导致编译错误。
同样值得注意的是,我在这个例子中使用的字符串文字在现实中是自定义的,gmock无法为其提供默认值(如bool(。
您的原始代码中有一些其他错误,问题中没有提到。如果您构造了一个最小的自包含示例,那么问题中提供的代码的行为与您预期的一样。
例如,以下代码:
#include <string>
#include "gmock/gmock.h"
using ::testing::Return;
struct MyClass {
virtual ~MyClass() {}
virtual std::string myMockMethod() = 0;
};
struct MyMockClass : MyClass {
MOCK_METHOD0(myMockMethod, std::string());
};
TEST(MyClass, Fails) {
MyMockClass myMockObject;
ON_CALL(myMockObject, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject, myMockMethod()).Times(99999);
myMockObject.myMockMethod();
}
TEST(MyClass, Passes) {
MyMockClass myMockObject;
EXPECT_CALL(myMockObject, myMockMethod()).Times(1).WillRepeatedly(Return("hello mock"));
myMockObject.myMockMethod();
}
int main(int argc, char **argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
产生以下(预期(输出:
[==========] Running 2 tests from 1 test case. [----------] Global test environment set-up. [==========] 2 tests from MyClass [ RUN ] MyClass.Fails ..srcmain.cc(18): error: Actual function call count doesn't match EXPECT_CALL(myMockObject, myMockMethod())... Expected: to be called 99999 times Actual: called once - unsatisfied and active [ FAILED ] MyClass.Fails (0 ms) [ RUN ] MyClass.Passes [ OK ] MyClass.Passes (0 ms) [----------] 2 tests from MyClass (2 ms total) [----------] Global test environment tear-down [==========] 2 tests from 1 test case ran. (2 ms total) [ PASSED ] 1 test. [ FAILED ] 1 test, listed below: [ FAILED ] MyClass.Fails 1 FAILED TEST
如果你想把你的模拟对象放在测试夹具中,你可以这样做:
class MyClassTest : public testing::Test {
protected:
MyMockClass myMockObject_;
};
TEST_F(MyClassTest, Fails) {
ON_CALL(myMockObject_, myMockMethod()).WillByDefault(Return("hello mock"));
EXPECT_CALL(myMockObject_, myMockMethod()).Times(99999);
myMockObject_.myMockMethod();
}
Mock::VerifyAndClearExpectations(&myMockObject);
这就成功了。我仍然不确定在幕后是如何管理期望的,但这让我开始工作。
不过,如有任何进一步的解释,我们将不胜感激。
相关文章:
- "error: no matching function for call to"构造函数错误
- 调用专用模板时出错"no matching function for call to [...]"
- 瓦尔格林德:数学函数"Conditional jump or move depends on uninitialised value(s)"
- "no matching function for call to 'Vector::Vector'"错误
- 我在 ifstream input_file(文件名)行中收到错误"no matching function to call";
- 模板规范获取'Ambiguous call to overloaded function'
- 循环挂起迭代的 std::擦除 on std::list
- SIGSEGV on Boost UDP 套接字关闭 - tcache_get at malloc.c.
- 从 TVector2 类派生复杂类:"No matching function for call to Complex::Complex()"
- CPP 中的瓦尔格林德和记忆泄漏:"Conditional jump or move depends on uninitialised values"
- std::bind on statd::array 的运算符 []
- 您将如何连接"on the fly"文本+整数并将其传递给函数?
- 如何修复输出日志中的"EnableInput can only be specified on a Pawn for its Controller"错误
- VS2019 - Sudo Remote Debugging on Linux with Cmake project
- Incomings Call with Android Sip stack in Embarcadero C++ bui
- 学习 c++,编译错误"no matching function for call"
- C++ library with Tensorflow on Android
- SFML 交叉编译 for Windows on Linux.
- How to recover system gcc compiler on centos 6
- Cmake with Flex/Bison on windows