I need to test the class with methods that are responsible for reading from the console.
Here is not exactly it, but the goal of the question is to understand, how to invoke a function inside base one (called exactly during test) and impact the values inside it correctly (via any references or pointers using googletest frameworks)
I tried this variant:
// basic getline_wrapper
bool my_getline(std::istream &input, std::string &output)
{
return static_cast<bool>(std::getline(input, output));
}
//mocked and testing
class MockGetline
{
public:
MOCK_METHOD(bool, my_getline, (std::istream&, std::string&));
};
TEST(GetlineMockTest, HandlesInput) {
// Mock object
MockGetline mock_getline;
std::istringstream input("Test line");
std::string output;
// Define the mock behavior: set `output` to "Test line" and return true
EXPECT_CALL(mock_getline, my_getline(testing::_, testing::_))
.WillOnce(testing::DoAll(SetArgReferee<1>("Test line"), Return(true)));
bool result = mock_getline.my_getline(input, output);
EXPECT_TRUE(result);
}
It works as expected, but how to apply it for class with fields or test it nested in another function?
Here is example:
std::string getline_wrapper(){
std::mystring result;
my_getline(cin, result);
return result;
}
// here is presented before my_getline implementation
int main() {
std::string my_result = getline_wrapper();
}
I see no way to interact with variavle std::string result inside getline wrapper, so i cannot assign the value to it with testing::DoAll(SetArgReferee<1>(“Test line”).
What would you suggest?