Google Test: EXPECT_CALL usage in complex situations

1.Simple google test EXPECT call example with one call of the tested function

tested code

1

2

3

4

5

6

7

8

9

10

11

12

#define MY_VALUE 5

void my_func_int()

{

    int ret = 10;

    if (10 == GetValueInt(MY_VALUE))

    {

        ret = 100;

        SetValue(MY_VALUE);

    }

}

single EXPECT_CALL

1

2

3

4

5

6

7

8

9

10

11

12

TEST(Test0, my_func_int)

{

    My_Mock mock;

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(1)

        .WillOnce(Return(10));

    EXPECT_CALL(mock, SetValue(_)).Times(1);

    my_func_int();

}

As you can see above, we have one call of function my_func_int so there will be a certain call of mocked function GetValueInt, so the EXPECT_CALL for this function is mandatory. 
Depending of the return value of this functions, we had to decide also if the function SetValue will be called. In this specific case, it is, so the EXPECT_CALL was used again for this function. 

Pretty simple, let's see a more complex example.

2.Double call of the tested function and the solution for EXPECT_CALL

In the next example, we will call our tested function my_func_int twice (NOTE: we will do that only for training purpose, in this case there is no other reason to do that):

NOT WORKING multiple EXPECT_CALL

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

TEST(Test1, my_func_int)

{

    My_Mock mock;

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(1)

        .WillOnce(Return(10));

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(1)

        .WillOnce(Return(5));

    EXPECT_CALL(mock, SetValue(_)).Times(1);

    my_func_int();

    my_func_int();

}

As you can see above, we changed the number of EXPECT_CALL for our mocked function GetValueInt, but this code will throw an error like this:

Output result (error)

u:\repository\fac_18s1\pkg\admin\test\gtest\prj\var1\tests\var1_test.cpp(88): error: Mock function called more times than expected - returning default value.

    Function call: GetValueInt(5)

          Returns: 0

         Expected: to be called once

           Actual: called twice - over-saturated and active

u:\repository\fac_18s1\pkg\admin\test\gtest\prj\var1\tests\var1_test.cpp(92): error: Actual function call count doesn't match EXPECT_CALL(mock, SetValue(_))...

         Expected: to be called once

           Actual: never called - unsatisfied and active

u:\repository\fac_18s1\pkg\admin\test\gtest\prj\var1\tests\var1_test.cpp(84): error: Actual function call count doesn't match EXPECT_CALL(mock, GetValueInt(_))...

         Expected: to be called once

           Actual: never called - unsatisfied and active

[  FAILED  ] Test1.my_func_int (3 ms)

The error above occurred because google test misinterprets the information which was given about all EXPECT_CALL. The way first two expect calls are used tells google test that there are two separated expect calls each called 1 time.
Actually there are two calls for the same function with same arguments, so an error is thrown. 

I will show you below some examples how to write the expect calls so that there will be no errors:

Solution 1

1

2

3

4

5

6

7

8

9

10

11

12

13

14

TEST(Test3, my_func_int)

{

    My_Mock mock;

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(2)

        .WillOnce(Return(10))

        .WillOnce(Return(5));

    EXPECT_CALL(mock, SetValue(_)).Times(1);

    my_func_int();

    my_func_int();

}

Solution 2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

TEST(Test4, my_func_int)

{

    My_Mock mock;

    {

        InSequence s;

        EXPECT_CALL(mock, GetValueInt(_))

            .Times(1)

            .WillOnce(Return(10));

        EXPECT_CALL(mock, SetValue(_)).Times(1);

        EXPECT_CALL(mock, GetValueInt(_))

            .Times(1)

            .WillOnce(Return(5));

    }

    my_func_int();

    my_func_int();

}

Solution 3

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

TEST(Test2, my_func_int)

{

    My_Mock mock;

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(1)

        .WillOnce(Return(10));

    EXPECT_CALL(mock, SetValue(_)).Times(1);

    my_func_int();

    EXPECT_CALL(mock, GetValueInt(_))

        .Times(1)

        .WillOnce(Return(5));

    my_func_int();

}

3.One more example

tested code

1

2

3

4

5

6

7

8

9

10

11

12

13

14

void my_func_int_2()

{

    int ret = 10;

    int temp = GetValueInt(14);

    int temp2 = GetValueInt(24);

    if (20 == GetValueInt(65))

    {

        ret = 100;

        SetValue(5);

    }

}

As you can see above, our function my_func_int_2 calls GetValueInt multiple times, but with different arguments. We will use that in our advantage to write the test code:

Solution for my_func_int_2

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

TEST(Test8, my_func_int_2)

{

    My_Mock mock;

    EXPECT_CALL(mock, GetValueInt(14))

        .Times(1)

        .WillOnce(Return(5));

    EXPECT_CALL(mock, GetValueInt(24))

        .Times(1)

        .WillOnce(Return(10));

    EXPECT_CALL(mock, GetValueInt(65))

        .Times(1)

        .WillOnce(Return(20));

    EXPECT_CALL(mock, SetValue(5)).Times(1);

    my_func_int_2();

}

In this way, all EXPECT_CALL have also the information about arguments of the function GetValueInt, so google test now can make the difference between them.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值