Optional parameters in google mocked method

2,754 views
Skip to first unread message

Amit Kulkarni

unread,
Oct 19, 2015, 3:03:08 PM10/19/15
to Google C++ Mocking Framework
Does google mock method support optional parameters?

I'm mocking a class. The original method is 

virtual bool create(const char* filename, const char* data=NULL, uint16 port=0);

And I derive from the base class to produce the mock class:

class MockBaseClass public BaseClass
{
public:
    BaseClass();

    MOCK_METHOD1( create, bool(const char* filename, const char* data, uint16 port) );
};

This produces an following error

: error C2118: negative subscript
: error C2660: 'testing::internal::FunctionMocker<Function>::Invoke' : function does not take 1 arguments
        with
        [
            Function=bool (const char *,const char *,uint16)
        ]

 : error C2660: 'testing::internal::FunctionMocker<Function>::With' : function does not take 1 arguments
        with
        [
            Function=bool (const char *,const char *,uint16)
        ]

If optional parameters are not supported by googlemock, is a solution to make my mock method work?

Regards
Amit

Samuel Benzaquen

unread,
Oct 19, 2015, 3:09:35 PM10/19/15
to Amit Kulkarni, Google C++ Mocking Framework
On Thu, Oct 15, 2015 at 1:53 PM, Amit Kulkarni <amitk...@gmail.com> wrote:
Does google mock method support optional parameters?

I'm mocking a class. The original method is 

virtual bool create(const char* filename, const char* data=NULL, uint16 port=0);

And I derive from the base class to produce the mock class:

class MockBaseClass public BaseClass
{
public:
    BaseClass();

    MOCK_METHOD1( create, bool(const char* filename, const char* data, uint16 port) );

The number in the macro name must match the number of parameters in the function signature.
This should be MOCK_METHOD3()
 
};

This produces an following error

: error C2118: negative subscript
: error C2660: 'testing::internal::FunctionMocker<Function>::Invoke' : function does not take 1 arguments
        with
        [
            Function=bool (const char *,const char *,uint16)
        ]

 : error C2660: 'testing::internal::FunctionMocker<Function>::With' : function does not take 1 arguments
        with
        [
            Function=bool (const char *,const char *,uint16)
        ]

If optional parameters are not supported by googlemock, is a solution to make my mock method work?

You can add an indirection:

class MockBaseClass {
 public:
  bool create(const char* filename, const char* data=NULL, uint16 port=0) {
    return mock_create(filename, data, port);
  }
  MOCK_METHOD3(mock_create, bool(const char*, const char*, uint16));
};
 
The create() method will still provide the default argument.
You set expectations in the mock_create() method instead.

_Sam


Regards
Amit

--

---
You received this message because you are subscribed to the Google Groups "Google C++ Mocking Framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to googlemock+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/googlemock/723f140b-4b84-43db-9dce-ca3ead53a2da%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages