From f77cac01033f0d67248c8b7b6e8a278acd42063d Mon Sep 17 00:00:00 2001 From: mat007 Date: Thu, 3 Oct 2013 07:59:17 +0000 Subject: [PATCH] Added documentation in the limitations section about why non-virtual methods cannot be mocked git-svn-id: https://svn.code.sf.net/p/turtle/code/trunk@693 860be788-9bd5-4423-9f1e-828f051e677b --- .../limitations_non_virtual_method.cpp | 19 +++++++++++++++++++ build/boost/doc/limitations.qbk | 17 +++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 build/boost/doc/example/limitations_non_virtual_method.cpp diff --git a/build/boost/doc/example/limitations_non_virtual_method.cpp b/build/boost/doc/example/limitations_non_virtual_method.cpp new file mode 100644 index 0000000..9d1123e --- /dev/null +++ b/build/boost/doc/example/limitations_non_virtual_method.cpp @@ -0,0 +1,19 @@ +#define BOOST_AUTO_TEST_MAIN +#include +#include + +//[ limitations_non_virtual_method_problem +class base +{ +public: + void method() // the method is not virtual + {} +}; +//] + +//[ limitations_non_virtual_method_problem_2 +MOCK_BASE_CLASS( mock_base, base ) +{ + MOCK_METHOD( method, 0 ) +}; +//] diff --git a/build/boost/doc/limitations.qbk b/build/boost/doc/limitations.qbk index 428673f..b5d93ca 100644 --- a/build/boost/doc/limitations.qbk +++ b/build/boost/doc/limitations.qbk @@ -1,6 +1,7 @@ [section Limitations] [import example/limitations_literal_zero.cpp] [import example/limitations_throw_specifier.cpp] +[import example/limitations_non_virtual_method.cpp] [import example/limitations_template_method.cpp] [import example/limitations_private_method.cpp] [import example/limitations_comma_in_macro.cpp] @@ -59,6 +60,22 @@ While still somewhat possible, mocking a template method is indeed a bit cumbers [endsect] +[section Non-virtual methods cannot be mocked] + +Given : + +[limitations_non_virtual_method_problem] + +the following code compiles but will not work as expected : + +[limitations_non_virtual_method_problem_2] + +The mock object will never be exercised because the library relies on polymorphism to hook the calls. + +There is no other solution than to refactor the production code, the most simple being to change the method to virtual. + +[endsect] + [section Private virtual methods cannot be mocked without spelling out the signature] Given :