使用 Google C++ Mocking Framework (Google Mock) (V1.5) 将任意参数传递给调用的方法 [英] Passing arbitrary arguments to invocked methods with Google C++ Mocking Framework (Google Mock) (V1.5)

查看:26
本文介绍了使用 Google C++ Mocking Framework (Google Mock) (V1.5) 将任意参数传递给调用的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模拟方法.当它被调用时,我希望它在调用其正常行为之前调用另一个函数.类似的东西:

I have a mock method. When it is called, I'd like it to call another function before calling its normal behavior. Something like :

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        Invoke(my_obj, &SomeAdditionalMethodIWantToCall),
        Invoke(my_obj, &DefaultBehavior),
        ));

唯一的问题是 SomeAdditionalMethodIWantToCall 期望的参数与提供给 MockedMethod 的参数完全无关.我希望能够给他们,但我在语法上挣扎.我希望有类似的东西(用假语法):

The only problem is that SomeAdditionalMethodIWantToCall expects parameter that not at all related to the one provided to MockedMethod. I'd like to be able to give them but I am struggling with the syntax. I wish there was something like (in fake syntax) :

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        Invoke(my_obj, &SomeAdditionalMethodIWantToCall, arg1, arg2, arg3),
        Invoke(my_obj, &DefaultBehavior),
        ));

我在文档中寻找过这样的东西,但没有成功.

I have looked for such a thing in the documentation without any success.

使用函数或函子作为动作中,我们有:

  • Invoke(f), Invoke(object_pointer, &class::method), InvokeWithoutArgs(f), InvokeWithoutArgs(object_pointer, &class::method) 它将在调用时仅转发(或不转发)提供给模拟函数的参数.

  • Invoke(f), Invoke(object_pointer, &class::method), InvokeWithoutArgs(f), InvokeWithoutArgs(object_pointer, &class::method) which will just forward (or not) the parameter provided to the mocked function when it is called.

InvokeArgument(arg1, arg2, ..., argk) 似乎用于调用参数之一.

InvokeArgument<N>(arg1, arg2, ..., argk) seems to be for calling one of the parameter.

复合操作

  • WithArg(a)WithArgs(a) 好像是从原始中选择哪些参数函数被转发.
  • WithArg<N>(a) and WithArgs<N1, N2, ..., Nk>(a) seem to be to select which parameters from the original function get forwarded.

我想我遗漏了一些很明显的东西,但我有点卡在这里,所以任何建议都会有所帮助.

I guess I am missing something quite obvious but I am a bit stuck here so any suggestion will help.

推荐答案

一种可能的选择是使用 Assign 操作将参数的值保存到类变量,然后使用一个帮手.使用以下期望:

One possible option would be to save the values of the arguments to class variables using the Assign action and then invoke the other function using a helper. Use the following expectation:

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        Assign(&classVariable1, arg1),
        Assign(&classVariable2, arg2),
        Assign(&classVariable3, arg3),
        InvokeWithoutArgs(my_obj, &MyClass::SomeAdditionalMethodIWantToCallHelper),
        Invoke(my_obj, &MyClass::DefaultBehavior),
        ));

然后定义你的辅助函数如下:

Then define your helper function as follows:

void MyClass::SomeAdditionalMethodIWantToCallHelper()
{
    SomeAdditionalMethodIWantToCall(classVariable1, classVariable2, classVariable3);
}

编辑

另一种可能的选择是编写一个自定义操作,该操作接收指向成员函数的指针以及您希望传递给它的参数.这更接近于您最初想要的语法.这是自定义操作:

Another possible option is to write a custom action that takes in a pointer to a member function along with the arguments you wish to pass to it. This is closer to what you originally desired for syntax. Here is the custom action:

ACTION_P5(InvokeUnrelatedFunction, classPointer, pointerToMemberFunc,
          first, second, third)
{
    (classPointer->*pointerToMemberFunc)(first, second, third);
}

以下是您在期望中使用它的方式:

Here's how you would use it in an expectation:

EXPECT_CALL(*my_obj, MockedMethod(_,_,_,_,_,_))
    .WillOnce(DoAll(
        InvokeUnrelatedFunction(my_obj, &MyClass::SomeAdditionalMethodIWantToCall,
                                arg1, arg2, arg3),
        Invoke(my_obj, &MyClass::DefaultBehavior),
        ));

这篇关于使用 Google C++ Mocking Framework (Google Mock) (V1.5) 将任意参数传递给调用的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆