[英]How to unit test the std::bind function using gtest?
I am trying to write unittest cases for some of the cpp files in my project. 我正在尝试为我的项目中的一些cpp文件编写unittest案例。
The scenario here is: I have a cpp file with only one public method defined and in turn which calls private methods. 这里的场景是:我有一个cpp文件,只定义了一个公共方法,然后调用私有方法。
Here the private methods are called in the public method as a callback method. 这里私有方法在public方法中作为回调方法调用。 How do I test the private methods here. 我如何在这里测试私有方法。 I will be doing the mocking for Callback pointer and I am not sure how to call the private method. 我将对Callback指针进行模拟,我不知道如何调用私有方法。
Please give me some suggestions how to call the private methods in this scenario without changing the source code. 请给我一些建议如何在不更改源代码的情况下调用此方案中的私有方法。
Here is the ex: 这是前者:
buttonListenerList <<
sourceButton->addButtonActionCallback(std::bind(&AudioSource::buttonCallback, this, _1, _2));
This peace of code is defined in the public method. 这种代码的和平是在公共方法中定义的。 Now the AudioSource::buttonCallback
is a private method. 现在AudioSource::buttonCallback
是一个私有方法。 How do you make sure to call this private method by calling the public method. 如何通过调用public方法确保调用此私有方法。
If, (you answered yes in comment) sourceButton
can be mocked - then expect addButtonActionCallback
is called - and store the passed std::function<>
arg. 如果,(你在评论中回答是肯定的)可以sourceButton
- 那么期望调用addButtonActionCallback
- 并存储传递的std::function<>
arg。
Like in the example below (replace ... with real types): 如下例所示(替换为真实类型):
struct TestSuite : public ::testing::Test {
... sourceButtonMock;
std::unique_ptr<...> objectUnderTest;
void SetUp() override;
std::function<...> sourceButtonActionCallback;
};
using namespace ::testing;
void TestSuite::SetUp()
{
EXPECT_CALL(sourceButtonMock, addButtonActionCallback(_))
.WillOnce(SaveArg<0>(&sourceButtonActionCallback);
objectUnderTest = std::make_unique<...>(sourceButtonMock);
}
Having stored this callback in sourceButtonActionCallback
member variable - you can call it freely wherever you wish: 将此回调存储在sourceButtonActionCallback
成员变量中 - 您可以随意在任何地方调用它:
TEST_F(TestSuite, shallDo...OnSourceButtonClick)
{
// prerequisites
ASSERT_NE(sourceButtonActionCallback, nullptr);
// Expecteations
...
// action
sourceButtonActionCallback(...);
// post-assertions
...
}
In Unit Testing you do not test the private member functions, just the public ones. 在单元测试中,您不测试私有成员函数,只测试公共成员函数。
I have a large project that uses a map
with a list of function
s that have parameters bound to them. 我有一个大型项目,它使用带有一系列function
的map
,这些function
的参数绑定到它们。 I call the public method with different parameters which in turn calls various functions in the map. 我用不同的参数调用public方法,这些参数又调用map中的各种函数。 I can then inspect the response states (error codes as this is multi-threaded) to verify the correct behaviour. 然后,我可以检查响应状态(错误代码,因为这是多线程)以验证正确的行为。 This is what you should do in your scenario. 这是您应该在您的方案中执行的操作。
Call the public function and inspect the state at the end. 调用公共函数并在最后检查状态。
I think you have to consider what you want tested. 我想你必须考虑你想要测试的东西。
Without seeing the code it's hard to comment further, but don't try and test if the button works (calls a callback). 没有看到代码,很难进一步评论,但不要尝试测试按钮是否工作(调用回调)。 No point there. 没有意义。
Therefore, the (private) callbacks modify state when they happen. 因此,(私有)回调会在发生时修改状态。 Expose (the immutable state) and assert that the state is as expected. 公开(不可变状态)并断言状态是预期的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.