简体   繁体   English

如何使用gtest对std :: bind函数进行单元测试?

[英]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. 我有一个大型项目,它使用带有一系列functionmap ,这些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. 我想你必须考虑你想要测试的东西。

  • If you want to test whether the callbacks will be called when the button is pushed, you needn't expose your private methods, in fact, you don't need to do that, as the button "is already proven". 如果你想测试按下按钮时是否会调用回调,你不需要公开你的私有方法,事实上,你不需要这样做,因为按钮“已经被证明”了。 When the button is pressed, associated callbacks are called. 按下按钮时,将调用相关的回调。
  • If you want to test whether your private functions do what they should, the outcome of the callback should modify some state (which can be exposed via getters). 如果你想测试你的私有函数是否做了他们应该做的事情,回调的结果应该修改一些状态(可以通过getter公开)。 Then you need a way to invoke the button event, or you could use a virtual function that can be exposed via button and test. 然后,您需要一种方法来调用按钮事件,或者您可以使用可以通过按钮和测试公开的虚拟功能。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM