C++ Google Mock 是一个很好的模拟框架吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2827319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:11:14  来源:igfitidea点击:

Is Google Mock a good mocking framework?

c++mockinggooglemock

提问by des4maisons

I am pioneering unit testing efforts at my company, and need need to choose a mocking framework to use. I have never used a mocking framework before. We have already chosen Google Test, so using Google Mock would be nice. However, my initial impressions after looking at Google Mock's tutorialare:

我在我的公司开创了单元测试工作,需要选择一个模拟框架来使用。我以前从未使用过模拟框架。我们已经选择了 Google Test,所以使用 Google Mock 会很好。但是,我看了Google Mock 的教程后的初步印象是:

  • The need for re-declaring each method in the mocking class with a MOCK_METHODn macro seems unnecessary and seems to go against the DRY principle.
  • Their matchers (eg, the '_' in EXPECT_CALL(turtle, Forward(_));) and the order of matching seem almost too powerful. Like, it would be easy to say something you don't mean, and miss bugs that way.
  • 需要使用 MOCK_METHODn 宏在模拟类中重新声明每个方法似乎没有必要,而且似乎违反了 DRY 原则。
  • 它们的匹配器(例如,EXPECT_CALL(turtle, Forward(_)); 中的“_”)和匹配顺序似乎太强大了。就像,很容易说出你不想说的话,那样会错过错误。

I have high confidence in google's developers, and low confidence in my own ability to judge mocking frameworks, never having used them before. So my question is: Are these valid concerns?

我对 google 的开发者有很高的信心,对自己判断 mocking 框架的能力缺乏信心,以前从未使用过它们。所以我的问题是:这些担忧是否合理?

Or is there no better way to define a mock object, and are the matchers intuitive to use in practice? I would appreciate answers from anyone who has used Google Mock before, and comparisons to other C++ frameworks would be helpful.

或者没有更好的方法来定义模拟对象,并且匹配器在实践中使用是否直观?我很感激以前使用过 Google Mock 的任何人的回答,并且与其他 C++ 框架进行比较会有所帮助。

回答by Stephen

I use it frequently.

我经常使用它。

It's trivial to do relatively easy things, and possible to do very difficult things - that's pretty much what I want from a framework.

做相对简单的事情是微不足道的,而做非常困难的事情是可能的——这几乎是我想要从框架中得到的。

The hardest part about writing custom Matchers (and other stuff) with Google's mocks isn't Google's mocks, it's C++'s template errors... they're close to impossible to parse. I often write complex expressions by incrementally building a working expression from a few less complicated expressions. That way, the template errors are easier to pinpoint.

使用 Google 的模拟编写自定义匹配器(和其他东西)最困难的部分不是 Google 的模拟,而是 C++ 的模板错误……它们几乎无法解析。我经常通过从一些不太复杂的表达式逐步构建一个工作表达式来编写复杂的表达式。这样,模板错误更容易查明。

I haven't seen a better option for c++ mocking, and Google's covers a lot of ground, so I'd suggest you give it a shot.

我还没有看到更好的 c++ 模拟选择,而且 Google 涵盖了很多领域,所以我建议你试一试。

WRT the DRY principle, I agree the declaring the mocked methods is unfortunate, but without reflection, I'm not sure c++ would have much luck otherwise. I'm near certain if there were a way, googlemock would be using it ;)

WRT DRY 原则,我同意声明模拟方法是不幸的,但如果没有反思,我不确定 c++ 是否会有很多运气。我几乎可以肯定,如果有办法,googlemock 会使用它;)

BTW: The googlemock cookbookis a good reference.

顺便说一句:googlemock 食谱是一个很好的参考。

回答by Eran Pe'er

Fake-Itis a simple mocking framework for C++. FakeIt uses the latest C++11 features to create an expressive (yet very simple) API. With FakeIt there is no need for re-declaring methods nor creating a derived class for each mock. Here is how you Fake-It:

Fake-It是一个简单的 C++模拟框架。FakeIt 使用最新的 C++11 特性来创建一个富有表现力(但非常简单)的 API。使用 FakeIt 不需要重新声明方法,也不需要为每个模拟创建派生类。这是你如何伪造它:

struct SomeInterface {
  virtual int foo(int) = 0;
};

// That's all you have to do to create a mock.
Mock<SomeInterface> mock; 

// Stub method mock.foo(any argument) to return 1.
When(Method(mock,foo)).Return(1);

// Fetch the SomeInterface instance from the mock.
SomeInterface &i = mock.get();

// Will print "1"
cout << i.foo(10);

There are many more features to explore. Go ahead and give it a try.

还有更多功能需要探索。去试试吧

回答by dascandy

Disclaimer: I wrote HippoMocks.

免责声明:我写了 HippoMocks。

I can recommend looking at other mocking frameworks; there's a class of them that don't make you repeat yourself. They also do away with a new syntax for matching making your code read much more like C++ combined with English. Give it a try!

我可以推荐查看其他模拟框架;有一类不会让你重复自己。它们还取消了用于匹配的新语法,使您的代码读起来更像 C++ 与英语的结合。试一试!

http://www.assembla.com/wiki/show/hippomocks

http://www.assembla.com/wiki/show/hippomocks

回答by Ian

I've been using googletest + googlemock professionally for a few years, and I definitely like it. One thing that hasn't been mentioned by others is that if you're already committed to using googletest then it makes a lot of sense to also use googlemock. They're fairly well integrated and shared a similar design style and philosophy, which is nice.

我已经专业地使用 googletest + googlemock 几年了,我绝对喜欢它。其他人没有提到的一件事是,如果您已经承诺使用 googletest,那么使用 googlemock 也很有意义。它们整合得相当好,共享相似的设计风格和理念,这很好。

For example, googlemock provides ASSERT_THAT()macros which are super-useful, and coexist nicely with googletests' assertions.

例如,googlemock 提供了ASSERT_THAT()非常有用的宏,并且可以与 googletests 的断言很好地共存。

I would caution you about abusing the power of googlemock, however. It can be extremelytempting to write very complex & powerful matchers that end up being totally unreadable. You just need to be disciplined when using it.

但是,我会警告您不要滥用 googlemock 的功能。编写非常复杂且功能强大的匹配器最终完全无法阅读是非常诱人的。你只需要在使用它时遵守纪律。

Some other thoughts:

其他一些想法:

  • Googlemock can have a somewhat steep learning curve; the intricacies of matchers and expectations are not as straight-forward as you might hope.
  • The concern about violating DRY is valid; it's annoying to have to manually define mocks when it seems like they could be easily auto-generated. It's fairly common for teams to write their own code generators that automatically define googlemocks for their interfaces.
  • Googlemock 的学习曲线有些陡峭;匹配器和期望的复杂性并不像您希望的那么简单。
  • 关于违反 DRY 的担忧是有道理的;当它们看起来很容易自动生成时,不得不手动定义模拟是很烦人的。团队编写自己的代码生成器来自动为其接口定义 googlemocks 是相当普遍的。