Search code examples
c++testinggoogletestgooglemock

Does gtest/gmock have a way to stub every instance of a class?


Is it possible to stub every instance of a class in C++ using GTest/GMock like RSpec has with any_instance? Read the docs but couldn't find anything on it.

Thanks.


Solution

  • Short answer: No. There is no such built-in functionality in gtest/gmock.

    Long answer: There is no way to impose certain behavior on an arbitrary C++ class, especially those in C++ standard library (C++ equivalent of Ruby core library). These are two fundamentally different languages. Ruby is a "message oriented" language, all objects respond to messages. Such concept does not exist in C++, so there is not way to achieve the behavior you desire.