Uninteresting mock function call bla() amp;amp; Expected: to be called at least once bla()?(无趣的模拟函数调用 bla() amp;amp;预期:至少被调用一次 bla()?)
问题描述
我用模拟类编写了一个小测试.当我运行它时,首先我得到一个警告,即调用了一个无趣的模拟函数,然后测试失败,因为没有满足期望,即模拟函数至少被调用一次.有趣的是,当我看到上面的警告消息时,调用了该函数.
I've written a small test with a mocked class. When I run it, first I get the warning that an uninteresting mock function was called and then the test fails because the expectation is not met, which is that the mocked function is called at least once. The funny thing is that that function is called as I see that warning message above.
你对这件事有什么想法吗?
Do you have any ideas on this matter?
谢谢!
这是我的代码结构:
class Bla {
public:
Bla();
virtual ~Bla();
virtual float myFunction();
}
class MockBla : public Bla {
MockBla();
~MockBla();
MOCKMETHOD0(myFunction, float());
}
class CallerClass {
public:
CallerClass() { MockBla* myMock = new MockBla(); }
virtual ~CallerClass();
myCallingFunction() { myMock->myFunction(); }
}
class MyTestClass : public ::testing::Test {
//....
TEST(myTest, testMyFunction) {
MockBla mockBla;
EXPECT_CALL(mockBla, myFunction()).Times(AtLeast(1));
CallerClass* callerClass;
callerClass = new CallerClass();
callerClass->myCallingFunction();
}
//....
}
结果:
[ RUN ] MyTestClass.testMyFunction
GMOCK WARNING:
Uninteresting mock function call - returning default value.
Function call: myFunction()
Returns: 0
Stack trace:
MyTestClass.cpp:99: Failure
Actual function call count doesn't match EXPECT_CALL(mockBla, myFunction())...
Expected: to be called at least once
Actual: never called - unsatisfied and active
推荐答案
您需要在测试执行期间调用的模拟类的实际实例上设置期望.
You need to set the expectations on the actual instance of your mocked class which will be called during the test's execution.
在您的情况下,您正在对对象 mockBla
设置期望,该对象仅在测试结束时构造然后销毁 - 它从未使用过.
In your case, you're setting the expectations on the object mockBla
which is only constructed then destructed at the end of the test - it is never used.
您需要将模拟对象传递给 CallerClass
以使用,或者允许 CallerClass
将模拟对象创建为成员变量,但随后允许对该实际成员的测试访问权限(例如通过 getter 或允许测试成为 CallerClass
的朋友).
You'll either need to pass the mock object into the CallerClass
to use, or allow the CallerClass
to create the mock object as a member variable, but then allow the test access to that actual member (via e.g. a getter or allowing the test to be a friend of the CallerClass
).
将模拟对象传递给调用类的示例如下:
An example of passing the mocked object into the calling class would be something like:
#include <memory>
#include "gmock/gmock.h"
class Bla {
public:
virtual ~Bla() {}
virtual float myFunction() = 0;
};
class MockBla : public Bla {
public:
MOCK_METHOD0(myFunction, float());
};
class CallerClass {
public:
explicit CallerClass(Bla* bla) : mBla(bla) {}
void myCallingFunction() { mBla->myFunction(); }
private:
Bla* mBla;
};
TEST(myTest, testMyFunction) {
std::shared_ptr<Bla> mockBla(new MockBla);
EXPECT_CALL(*std::static_pointer_cast<MockBla>(mockBla),
myFunction()).Times(testing::AtLeast(1));
CallerClass callerClass(mockBla.get());
callerClass.myCallingFunction();
}
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
这篇关于无趣的模拟函数调用 bla() &&预期:至少被调用一次 bla()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无趣的模拟函数调用 bla() &&预期:至少被调用一次 bla()?
基础教程推荐
- 调用std::Package_TASK::Get_Future()时可能出现争用情况 2022-12-17
- C++,'if' 表达式中的变量声明 2021-01-01
- C++ 程序在执行 std::string 分配时总是崩溃 2022-01-01
- 什么是T&&(双与号)在 C++11 中是什么意思? 2022-11-04
- 如何定义双括号/双迭代器运算符,类似于向量的向量? 2022-01-01
- 设计字符串本地化的最佳方法 2022-01-01
- 如何在 C++ 中处理或避免堆栈溢出 2022-01-01
- 运算符重载的基本规则和习语是什么? 2022-10-31
- C++ 标准:取消引用 NULL 指针以获取引用? 2021-01-01
- 您如何将 CreateThread 用于属于类成员的函数? 2021-01-01