【发布时间】:2018-05-19 17:51:47
【问题描述】:
如何使用 googletest 测试依赖于用户通过 std::cin 输入的函数?
对于下面的示例,我正在寻找允许我将“2\n”添加到 std::cin 流的任何代码,以便 readUserInput() 函数将 2 读入 value 变量并且不需要用户的任何输入。
#include <iostream>
#include "gtest/gtest.h"
int readUserInput()
{
int value;
std::cout << "Enter a number: ";
std::cin >> value;
return value;
}
TEST(cin_test, Basic)
{
// need code here to define "2\n"
// as the next input for std::cin
ASSERT_EQ(readUserInput(), 2);
}
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
【问题讨论】:
标签: c++ iostream googletest cin