Google模拟ByRef方法

编程入门 行业动态 更新时间:2024-10-10 12:23:19
本文介绍了Google模拟ByRef方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个将布尔值作为参考参数并返回整数的类:

I have a class that takes a boolean as a reference parameter and returns an integer:

class Foo { public: Bar my_bar; virtual int myMethod(bool &my_boolean) = 0; } /*...*/ int Foo::myMethod(bool &my_boolean){ if (my_bar == NULL){ my_boolean = false; return -1; } else{ my_boolean = true; return 0; } }

我为此课程创建了一个模拟游戏:

And I created a mock for this class:

class MockFoo : public Foo { MOCK_METHOD1(myMethod,int(bool &my_boolean)); }

我在如何设置对此类功能的期望方面遇到问题,因为我需要将返回值和参考参数设置为特定值以正确创建我的单元测试.我可以用gmock处理这种​​功能吗?我尝试按照我认为是文档中的解决方案的方法进行操作:

I'm having problems on how to set the expectations for this kind of function,because I need to set the return value and the reference parameter to specific values to properly create my unit tests.How can I deal with this kind of function with gmock?I tried following what I thought was the solution on the documentation:

using ::testing::SetArgPointee; class MockMutator : public Mutator { public: MOCK_METHOD2(Mutate, void(bool mutate, int* value)); ... }; ... MockMutator mutator; EXPECT_CALL(mutator, Mutate(true, _)) .WillOnce(SetArgPointee<1>(5));

但是我要么不理解该示例,要么不适用于此案例.以前有没有人处理过这种情况?

But either I didn't understood the example or it wasn't applicable for this case.Has anyone dealt whith this kind of situation before?

谢谢.

推荐答案

您的问题很难得到!谷歌模拟烹饪书"中的示例也是如此.

Your question is hard to get! The samples from google mocks 'cook book' are so as well.

您是否要在模拟类中重用Foo::myMethod()的实现,还是只想模拟特定调用情况的副作用(返回值并由ref参数更改)?

Do you want to reuse the implementation for Foo::myMethod() with you mock class, or do you just want to mock the side effects (return value and changed by ref parameters) for specific call situations?

模拟类通常是用来替换/模拟您的Foo类,而不是直接继承它或它的行为.不知道您为纯方法定义此默认"行为的方式是否可行,但对此表示怀疑.然后,您可以简单地省略= 0. 更好的方法是分离出一个真实的接口声明,例如:

A mock class is usually meant to replace / simulate your Foo class, not to inherit it directly or it's behavior. Don't know if the way you define this 'default' behavior for a pure method will work, but doubt that. You might simply omit the = 0 then. The better approach would be to separate out a real interface declaration like:

struct IFoo { virtual int myMethod(bool &my_boolean) = 0; virtual ~IFoo() {} }; class Foo : public IFoo { // ... }; class MockFoo : public IFoo { MOCK_METHOD1(myMethod,int(bool &my_boolean)); };

如果是后一种情况,则应该使用testing::Return(value)和testing::SetArgReferee<N>(value)下车(在非常有用的备忘单" ).

If you have the latter case, you should get off with testing::Return(value) and testing::SetArgReferee<N>(value) (found that in the very useful 'Cheat Sheet').

您的期望电话应如下所示:

Your expectation call should look like this then:

MockFoo foo; // Expect call to myMethod() return -1 and set the by ref argument to true EXPECT_CALL(foo, myMethod(_)) .WillOnce(DoAll(SetArgReferee<0>(true),Return(-1))); // Expect call to myMethod() return 0 and set the by ref argument to false EXPECT_CALL(foo, myMethod(_)) .WillOnce(DoAll(SetArgReferee<0>(false),Return(0)));

如果您真的想为myMethod()重用原始的类逻辑,请查看分别委托给父类的委托" . '委派调用真实对象"

If you really want to reuse your original classes logic for myMethod() have a look at 'Delegating calls to a parent class', resp. 'Delegating Calls to a Real Object'

更多推荐

Google模拟ByRef方法

本文发布于:2023-11-03 23:42:27,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Google   ByRef

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!