C ++代码如下;
Student.hpp
class Student
{
private:
string name;
int roll_no;
int marks;
public:
Student() {};
virtual ~Student(){};
virtual void getDetails() = 0;
virtual void printDetails() = 0;
virtual int getMarks() = 0;
};
mockStudent.hpp:
class MockStudent : public Student
{
public:
MOCK_METHOD0(getDetails, void());
MOCK_METHOD0(printDetails, void());
MOCK_METHOD0(getMarks, int(void));
};
main.cpp:
TEST(StudentTest, MarksTest)
{
// This test is named "MarksTest", and belongs to the "StudentTest"
// test case.
MockStudent s2;
EXPECT_CALL(s2, getMarks())
.Times(AtLeast(1));
cout << s2.getMarks();
}
GTEST_API_ int main(int argc, char** argv)
{
// The following line must be executed to initialize Google Mock
// before running the tests.
::testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
上面的代码已成功编译并运行它。 结果如下:
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from StudentTest
[ RUN ] StudentTest.MarksTest
[ OK ] StudentTest.MarksTest (0 ms)
[----------] 1 test from StudentTest (0 ms total)
[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
我怀疑。我在哪里可以实现我的函数定义,例如getMarks()等。 我试图在mockstudent.cpp(新文件)中添加定义,但它显示错误&#34;重新定义函数&#34;。 如果我遗漏任何概念,请原谅我。 提前谢谢。
答案 0 :(得分:0)
我相信你正在寻找Setting Expectations和类似的东西:
EXPECT_CALL(s2, getMarks())
.Times(AtLeast(1))
.WillOnce(Return(1));