Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
October 25, 2022 01:41 pm GMT

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:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To