I have a class Foo. I only want to allow Bar to create it, so I make its constructor private and make Bar its friend. But now how can I unit test Foo's public methods (e.g., getValue)? I do not have a way to create its instance in the unit test file.
class Foo final
{
public:
int getValue();
private:
friend class Bar;
Foo(int value);
};
class Bar
{
protected:
Foo createFoo(int value);
};
You can add an extra friend class for Unit testing
class Foo final
{
public:
int getValue();
private:
friend class Bar;
friend class UnitTests; // Here
Foo(int value);
};
class UnitTests {
public:
bool constructor_ok() { Foo test_instance(42); ... }
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With