caiqinzhou / googlemock

Automatically exported from code.google.com/p/googlemock
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

need documentation on mocking C++ operators #53

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Create interface with virtual operator (e.g. operator==())
2. Create mock class implementing interface
3. Create instance of mock class

What is the expected output? What do you see instead?
I'd expect it to compile cleanly and produce the usual output

What version of the product are you using? On what operating system?
Googlemock 1.1 on Windows XP (MSVC 8)

Please provide any additional information below.

    using namespace testing;
    class IAm
    {
    public:
        virtual bool operator==(const IAm&) = 0;
    };

    class MockIAm : public IAm
    {
    public:
        MOCK_METHOD1(operator==, bool(const IAm&));
    };

    TEST(test, test1)
    {
        MockIAm iam;
    }

results in:
c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp(3
7)
: error C2143: syntax error : missing ';' before '=='
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2334: unexpected token(s) preceding '{'; skipping apparent
function body
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2143: syntax error : missing ';' before '=='
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2040: '`anonymous-namespace'::MockIAm::gmock_operator' :
'testing::internal::FunctionMocker<Function>' differs in levels of
indirection from 'testing::internal::MockSpec<F> &'
1>        with
1>        [
1>            Function=bool (const `anonymous-namespace'::IAm &)
1>        ]
1>        and
1>        [
1>            F=bool (const `anonymous-namespace'::IAm &)
1>        ]
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2238: unexpected token(s) preceding ';'
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2065: '_37' : undeclared identifier
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2228: left of '.SetOwnerAndName' must have class/struct/union
1>        type is ''unknown-type''
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(37)
: error C2228: left of '.Invoke' must have class/struct/union
1>        type is ''unknown-type''
1>c:\ccm\ukedersdb\platinum\products\doors\src\jazz_proxy_test\resource_test.cpp
(42)
: error C2512: '`anonymous-namespace'::MockIAm' : no appropriate default
constructor available
1>

Original issue reported on code.google.com by organic...@googlemail.com on 23 Jun 2009 at 2:26

GoogleCodeExporter commented 9 years ago
This can be done by delegating the operator call to a named method.  I'll write 
up a
recipe for this soon.

Original comment by zhanyong...@gmail.com on 23 Jun 2009 at 4:53

GoogleCodeExporter commented 9 years ago
I realise delegation can be used as a workaround. However, consider an 
interface with
one (or more) pure virtual operators. How can one define a mock that can be 
instantiated?

Original comment by organic...@googlemail.com on 23 Jun 2009 at 8:12

GoogleCodeExporter commented 9 years ago
Example:

class IAm {
 public:
  virtual ~IAm() {}
  virtual bool operator==(const IAm&) = 0;
};

class MockIAm : public IAm {
 public:
  MOCK_METHOD1(Equals, bool(const IAm&));
  virtual bool operator==(const IAm& rhs) { return Equals(rhs); }
};

MockIAm can be instantiated.  You should set expectations on Equals()
instead of operator==:

  MockIAm x;
  EXPECT_CALL(x, Equals(_));

BTW, for questions and feature requests instead of bug reports, it
works better to use the discussion group.  (Very few people are paying
attention to the comments in the issue tracker, so you may wait for a
long time to get an answer if you post your questions here.)

Original comment by zhanyong...@gmail.com on 9 Aug 2009 at 1:46

GoogleCodeExporter commented 9 years ago
I shall add a recipe to the cookbook.

Original comment by w...@google.com on 6 Mar 2010 at 5:53

GoogleCodeExporter commented 9 years ago
Hi,

Is there any way to mocking a templatized functor??

Original comment by rodrigue...@gmail.com on 8 Jan 2015 at 8:41