是否可以使用gtest或gmock模拟一个被调用的函数返回值?

时间:2017-05-30 07:51:54

标签: c++ unit-testing code-coverage googletest googlemock

我是gtestgmock的新手,请让我了解如何模拟被调用的函数。这对我的代码覆盖也有帮助。

#include <stdio.h>
#include "gtest/gtest.h"

int ret()
{
    return 5;
}

class A
{
    public:
        A();
        int a;
        int func();
};

A::A()
{
    printf("This is constructor\n");
}

int A::func()
{
    int iRet = ret(); /* When func() is called by gtest I would like to control the return value of ret() */
    int iRet1 = ret();
    /* Based on these two values some operations to be done */
    printf("This is func. %d, %d\n", iRet, iRet1);
    return iRet;
}

TEST (abc, xyz) {
    A a;
    EXPECT_EQ(5, a.func()); /* Here how to get different values of iRet and iRet1 for ret() function? */
}

int main(int argc, char **argv)
{
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

如果无法通过gtest和/或gmock,请建议我使用其他工具。

此外,我尝试使用以下未按预期工作的内容:

int ret()
{
    printf("Returning 5\n");
    return 5;
}

int ret1()
{
    int iRet = 10;
    printf("Inside ret1\n");
    iRet = ret();
    if (iRet == 5)
    {
        printf("Original ret is called\n");
    }
    else if (iRet == 100)
    {
        printf("This is mocked function call\n");
    }
    else
    {
        printf("Opps! This should not happen\n");
    }
    return iRet;
}

class FooMock {
    public:
        MOCK_METHOD0(ret, int());
        MOCK_METHOD0(ret1, int());
};

TEST (abc, xyz) {
    FooMock mock;
    EXPECT_CALL(mock, ret()).Times(1).WillOnce(Return(100));
    mock.ret1();
}
int main(int argc, char **argv)
{
    ::testing::InitGoogleMock(&argc, argv);
    return RUN_ALL_TESTS();
}

在跑步时给我以下错误:

$ ./a.out 
[==========] Running 1 test from 1 test case.
[----------] Global test environment set-up.
[----------] 1 test from abc
[ RUN      ] abc.xyz
gtest.cpp:86: Failure
Actual function call count doesn't match EXPECT_CALL(mock, ret())...
         Expected: to be called once
           Actual: never called - unsatisfied and active
gtest.cpp:87: Failure
Actual function call count doesn't match EXPECT_CALL(mock, ret())...
         Expected: to be called once
           Actual: never called - unsatisfied and active
[  FAILED  ] abc.xyz (0 ms)
[----------] 1 test from abc (0 ms total)

[----------] Global test environment tear-down
[==========] 1 test from 1 test case ran. (0 ms total)
[  PASSED  ] 0 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] abc.xyz

 1 FAILED TEST

如果我做错了,请告诉我......

2 个答案:

答案 0 :(得分:0)

因此,单元测试的想法是你运行一些代码,并将结果与​​你期望的结果进行比较。您的代码使它变得更加复杂。 E.g。

#include <stdio.h>
#include "gtest/gtest.h"

int incBy5(const int a) {
  return a+5;
}

TEST (abc, xyz) {
  int x=17;
  EXPECT_EQ(x+5, incBy5(x));
}

int main(int argc, char **argv)
{
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}

一般参见单元测试概念的文档,特别是google-test。它的模拟方面都有了更复杂的东西来测试你可能没有一切可用的东西(例如网络资源,数据库......),你创建了可以取代这些资源的模型。

答案 1 :(得分:0)

在第二个代码段中,您定义了两个模拟方法retret1。之后,您在ret上设置了期望,但调用了ret1。在测试结束时,对ret的期望仍未实现,导致gmock抱怨。