Google Test中没有模拟的EXPECT_CALL [英] EXPECT_CALL without mock in Google Test
问题描述
是否可以通过GoogleTest for c ++测试函数调用而无需创建模拟对象,例如我们有以下生产代码:
Is there any way to test a function call via GoogleTest for c++ without creating mock object, e.g. we have the following production code:
if (a)
method(x);
我想测试在a
为True和a
为False的情况下是否会调用method
.我想构建一个与Google Test的EXPECT_CALL完全相同的测试,但是EXPECT_CALL仅适用于模拟对象的方法.就我而言,我不想使用模拟(不需要创建任何对象).
I would like to test whether the method
will be called in the case a
is True and a
is False. I would like to construct a test that does exactly the same what Google Test's EXPECT_CALL does, but EXPECT_CALL works only with the mock object's method. In my case I would prefer not to use a mock (there is no need to create any object).
推荐答案
按状态此处,
可以使用Google Mock模拟一个免费函数(即C样式函数或静态方法).您只需要重写代码即可使用接口(抽象类).
It's possible to use Google Mock to mock a free function (i.e. a C-style function or a static method). You just need to rewrite your code to use an interface (abstract class).
他们的可能" 具有误导性,因为您必须重写代码才能使用类(抽象或由模板提供),因此不再使用自由函数.
Their "It's possible" is misleading, as you have to rewrite code to use class (abstract, or provided by template), and so no longer use free functions.
这篇关于Google Test中没有模拟的EXPECT_CALL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!