我的测试夹具中有一个受保护的静态方法,我希望从辅助函数调用,而不是从单元测试函数本身调用。
class Fixture
{
...
protected:
static void fixture_func( int foo );
};
void helper_func( int bar ) {
Fixture::fixture_func( bar );
}
TEST_F( Fixture, example_test ) {
fixture_func( 0 ); //Line 1: This is how you would normally call the method
helper_func( 0 ); //Line 2: This is how I need to call the method
}
当我尝试第2行时,我显然得到了一个错误,即该方法"不可访问",因为它是fixture
中受保护的方法。我如何以某种方式将测试夹具传递给helper_func
,或者将fixture_func
置于helper_func
的范围内?
如果你想知道,简单地从单元测试本身调用fixture func
是不可行的,因为我正在设计一个测试框架,旨在简化fixture_func用于特定目的的使用。我也没有能力对fixture
进行非琐碎的更改。
无论该方法是static
还是调用函数是C样式的,都不能从类外部调用protected
或private
方法。
在任何情况下,在fixture_func
:之前都需要类作用域Fixture::
void helper_func( int bar ) {
Fixture::fixture_func( bar );
}
你需要以某种方式使fixture_func
变成public
,你可以尝试:
class FixtureExpanded : public Fixture { };
void helper_func( int bar ) {
FixtureExpanded::fixture_func( bar );
}