gmock具有11个参数的模拟方法 [英] mock method with 11 parameters with gmock

查看:156
本文介绍了gmock具有11个参数的模拟方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用gmock模拟遗留代码中的依存关系.其中一个类具有带有11个参数的方法.当我尝试使用MOCK_METHOD11_WITH_CALLTYPE对其进行模拟时,我发现此宏不存在. gmock最多只能支持10个参数.您对此有何建议?我是否可以用虚拟人体来实现此方法?或复制&扩展宏?谢谢!

I'm using gmock to mock my dependencies in legacy code. One of the class have a method with 11 parameters. When I tried to use MOCK_METHOD11_WITH_CALLTYPE to mock it, I found this macro doesn't exist. gmock only supoort up to 10 parameters. What do you suggest for this? Do I implement this method with dummy body? Or copy & extend the macro? Thanks!

PS,我现在不需要在测试中模拟此方法,但将来可能需要这样做.

PS, I don't need to mock this method in my tests right now, but probably need to do so in the future.

最诚挚的问候,

推荐答案

参数超过10个的方法可能会带来麻烦.我可以建议一种解决方法,该方法可以帮助您解决特定情况,但除了模拟之外,这也可能是一个好主意.将几个有意义的参数作为一个组,并将它们聚合到一个结构中.然后将该结构的实例作为参数传递给该方法.因此,您可能拥有3或4个参数,而不是11个参数.这不仅可以解决您遇到的模拟库问题,而且还可以提高类的可用性,因为带有这么多参数的方法通常很难在其中读取.呼叫站点.

Methods with more than 10 parameters may be a sign of trouble. I can suggest a workaround which will help your specific case but which may also be a good idea apart from mocking. Take several of the parameters that make sense as a group, and aggregate them in a struct. Then pass an instance of that struct as an argument to the method. So instead of 11 arguments you might then have 3 or 4. Not only does this help with the mock library problem you're having, it may improve the usability of your class, since methods with so many arguments are usually difficult to read at the call site.

这篇关于gmock具有11个参数的模拟方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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