An Interest In:
Web News this Week
- April 3, 2024
- April 2, 2024
- April 1, 2024
- March 31, 2024
- March 30, 2024
- March 29, 2024
- March 28, 2024
October 25, 2022 01:41 pm GMT
Original Link: https://dev.to/tandrieu/tiny-tips-testing-protected-method-without-using-friendship-in-c-32jk
Tiny Tips : Testing protected method without using friendship in C
Assume you have this class to test:
MyClass.h
class MyClass {public: float publicMethod();protected: float criticalInternalMethod();};
criticalInternalMethod()
implement a complex business logic or algorithm, but it is too internal to make it part of public API. How to test it ?
With GoogleTest, you can use the FRIEND_TEST macro. But it requires putting google header in public API, and a macro in class definition.
What you can do instead is this:
MyClassTest.cpp
class MyClassTest : public MyClass{public: using MyClass::criticalInternalMethod;};TEST(MyClassShould, computeGoodResult){ MyClassTest myClass; EXPECT_EQ(myClass.criticalInternalMethod(), 12);}
This doesn't work for private method, but at least it avoids polluting public header only for the sake of testing.
Original Link: https://dev.to/tandrieu/tiny-tips-testing-protected-method-without-using-friendship-in-c-32jk
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To