您现在的位置是:首页 > 电脑 > 

我可以使用谷歌模拟检查方法参数而无需提前设定预期吗?(Can I use google mocks to check method parameters without setting an expectation in advance?)

2025-07-22 09:28:05
我可以使用谷歌模拟检查方法参数而无需提前设定预期吗?(Can I use google mocks to check method parameters without setting an expectation in advance?) 我有一种情况,我想检查是否使用参数X调用模拟对象方法,但测试只有在调用模拟后才能访问X,所以我不能事先设置EXPECT_C
我可以使用谷歌模拟检查方法参数而无需提前设定预期吗?(Can I use google mocks to check method parameters without setting an expectation in advance?)

我有一种情况,我想检查是否使用参数X调用模拟对象方法,但测试只有调用模拟才能访问X,所以我不能事先设置EXPECT_CALL。

例如

// The class I'm testing. class Maker { void register(Listener& lis); Obj& make() { // make new Obj o // call created(o) on registered Listener // return o } } class Listener { virtual void created(Obj& o) = 0; } // The test Listener lis; Maker maker; maker.register(lis); Obj& o = (); // Check that lis was invoked using param o...how?

我可以用谷歌嘲笑吗? 使用谷歌模拟最优雅/可读的方式是什么?

显然我可以创建自己的MockListener来记录调用参数,而不是使用谷歌模拟。 但我希望google mocks会提供一种更易读的机制,类似于EXPECT_CALL。

I have a situation where I want to check if a mock object method was called with parameter X, but the test gets access to X only after the mock is invoked, so I can't set an EXPECT_CALL beforehand.

E.g.

// The class I'm testing. class Maker { void register(Listener& lis); Obj& make() { // make new Obj o // call created(o) on registered Listener // return o } } class Listener { virtual void created(Obj& o) = 0; } // The test Listener lis; Maker maker; maker.register(lis); Obj& o = (); // Check that lis was invoked using param o...how?

Can i do this with google mocks? What is the most elegant / readable way of doing this using google mocks?

Obviously I can make my own MockListener which will record invocation parameters, instead of using google mocks. But I'm hoping google mocks would preesnt a more readable mechanism, similar to EXPECT_CALL.

最满意答案

您可以使用SaveArg<>操作来保存调用Listener::created(Obj&)的参数值,这样您就可以将其值与()之后返回的值进行比较。

这将要求您为类Obj提供相等运算符,即bool operator==(ct Obj&, ct Obj&) 。

您的测试可能如下所示:

class ListenerMock : public Listener { public: MOCK_METHOD1(created, void(Obj&)); }; TEST(MakerTest, make_registersObject) { ListenerMock lis; Maker maker; maker.register(lis); Obj createdArg; EXPECT_CALL(lis, created(_)) .Times(1) .WillOnce(SaveArg<0>(&createdArg)); Obj& o = (); ASSERT_EQ(createdArg, o); }

You can use the SaveArg<> action to save the value of the parameter with which Listener::created(Obj&) is called, so you can compare its value to the one returned by () afterwards.

This will require that you provide equality operator for class Obj, bool operator==(ct Obj&, ct Obj&).

Your test could then look like this:

class ListenerMock : public Listener { public: MOCK_METHOD1(created, void(Obj&)); }; TEST(MakerTest, make_registersObject) { ListenerMock lis; Maker maker; maker.register(lis); Obj createdArg; EXPECT_CALL(lis, created(_)) .Times(1) .WillOnce(SaveArg<0>(&createdArg)); Obj& o = (); ASSERT_EQ(createdArg, o); }

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/79509.html

相关标签:无
上传时间: 2023-04-28 04:54:42
留言与评论(共有 20 条评论)
本站网友 北京出入境管理局
13分钟前 发表
MOCK_METHOD1(created
本站网友 芋头怎么吃
28分钟前 发表
make_registersObject) { ListenerMock lis; Maker maker; maker.register(lis); Obj createdArg; EXPECT_CALL(lis
本站网友 exp是什么意思
15分钟前 发表
but the test gets access to X only after the mock is invoked
本站网友 白塞氏综合征
24分钟前 发表
本站网友 心蓝数据
24分钟前 发表
class ListenerMock
本站网友 济南万泰
25分钟前 发表
void(Obj&)); }; TEST(MakerTest
本站网友 降压药分类
4分钟前 发表
本站网友 登陆进程初始化失败
13分钟前 发表
本站网友 比亚迪车型
27分钟前 发表
MOCK_METHOD1(created
本站网友 外国成人网站网址
27分钟前 发表
但测试只有在调用模拟后才能访问X
本站网友 企业创新
27分钟前 发表
ct Obj&). Your test could then look like this
本站网友 汇佳桌游
3分钟前 发表
void(Obj&)); }; TEST(MakerTest
本站网友 晚餐菜谱
14分钟前 发表
similar to EXPECT_CALL. 最满意答案 您可以使用SaveArg<>操作来保存调用Listener
本站网友 serialized
3分钟前 发表
而不是使用谷歌模拟
本站网友 宝宝不吃奶粉
19分钟前 发表
ct Obj&). Your test could then look like this
本站网友 青岛房价网
27分钟前 发表
void(Obj&)); }; TEST(MakerTest
本站网友 网金2
20分钟前 发表
所以我不能事先设置EXPECT_CALL
本站网友 广告法案例
16分钟前 发表
这样您就可以将其值与()之后返回的值进行比较
本站网友 逐步
19分钟前 发表
MOCK_METHOD1(created