jeudi 20 février 2020

Mocking a non-virtual function using Google Mock?

I am trying to mock the function sub so that I can test the function add.I am using non-virtual function,

//Non_virtual function
class baseclass {
public:
    int add(int a, int b) {
        return (a + sub(a, b));
    }
    int sub(int c, int d) {
        return (c - d);
    }
};
class mockclass {
public:
    MOCK_METHOD2(sub, int(int a, int b));

};
TEST(sample_test, testmain) {
    mockclass mo;
    int c = 12;
    int d = 4;
    EXPECT_CALL(mo, sub(c, d))
        .WillOnce(testing::Return(8));
    EXPECT_EQ(mo.add(c, d), 20);
}

I don't know how to make the relationship between the add and sub and don't know where I was making mistake. I can do it with virtual function but I want to do it in non-virtual function. Thanks in advance

Aucun commentaire:

Enregistrer un commentaire