EXPECT_CALL without mock in Google Test

人盡茶涼 提交于 2019-12-24 02:10:04

问题


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);

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).


回答1:


As state here,

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.




回答2:


If you are trying to fake a free function, you may want to look into the Fake Function Framework (fff). It allows you to replace free functions with fake implementations that can be used in a similar way to GoogleMock.

It works seamlessly with GoogleMock and GoogleTest.



来源:https://stackoverflow.com/questions/50750047/expect-call-without-mock-in-google-test

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!