简体   繁体   English

Google Test中没有模拟的EXPECT_CALL

[英]EXPECT_CALL without mock in Google Test

Is there any way to test a function call via GoogleTest for c++ without creating mock object, eg we have the following production code: 有什么方法可以通过GoogleTest for c ++测试函数调用而无需创建模拟对象,例如,我们具有以下生产代码:

if (a)
    method(x);

I would like to test whether the method will be called in the case a is True and a is False. 我想测试在a为True和a为False的情况下是否将调用该method 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. 我想构建一个与Google Test的EXPECT_CALL完全相同的测试,但是EXPECT_CALL仅适用于模拟对象的方法。 In my case I would prefer not to use a mock (there is no need to create any object). 就我而言,我不希望使用模拟(不需要创建任何对象)。

As state here , 作为这里的状态,

It's possible to use Google Mock to mock a free function (ie a C-style function or a static method). 可以使用Google Mock模拟自由函数(即C样式函数或静态方法)。 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. 它们的“可能”具有误导性,因为您必须重写代码才能使用类(抽象的或由模板提供),因此不再使用自由函数。

If you are trying to fake a free function, you may want to look into the Fake Function Framework (fff) . 如果您试图伪造自由函数,则可能需要研究伪函数框架(fff) It allows you to replace free functions with fake implementations that can be used in a similar way to GoogleMock. 它允许您用伪造的实现替换免费功能,这些伪造可以类似于GoogleMock的方式使用。

It works seamlessly with GoogleMock and GoogleTest. 它可与GoogleMock和GoogleTest无缝集成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM