The mock function has no default action set, and its return type has no default value set

20,793 views
Skip to first unread message

vdevarap

unread,
Jul 8, 2010, 3:59:29 PM7/8/10
to Google C++ Mocking Framework

Hi,

I am using G mock and g test to unit test my code.

I did not expect a call to one of the mock methods. so i used Nice
Mock to avoid the warning.
still the test is failing with the below error:

: Failure

The mock function has no default action set, and its return type
has no default value set.
Aborted


any help is appreciated.



Regards,
vdevarap

Amanda Robinson

unread,
Jul 8, 2010, 4:58:12 PM7/8/10
to vdevarap, Google C++ Mocking Framework
If you expect no call, then you should test for that and use StrictMock.

If you don't care, you still need to set a default response:
ON_CALL(MockClass, DoSomething(int))
  .WillRepeatedly(Return(0));

vdevarap

unread,
Jul 8, 2010, 5:42:27 PM7/8/10
to Google C++ Mocking Framework

Hi Amanda,

Thanks for quick reply.

i tried doing what you have suggested but still it is throwing the
same error.

I have mocked methods in my interface class which is abstract.

now in one of the tests i expect a call to my mock method which
returns a reference to other interface method.

how can i test this scenario.

here is the sample code:

class iface_a
{
public:
virtual ~iface_a() {};
virtual pthmgmt::iface_b& Getiface_b() =0;
};

class mock_iface_a : public iface_a
{
public:
virtual ~mock_iface_a() { };

MOCK_METHOD0(Getiface_b, pthgmt::iface_b&());

};


now when i am trying to check the invocation as below:

EXPECT_CALL(obj_mock_iface_a, Getiface_b)
.Times(Atleast(1));



this is throwing the error i mentioned earlier.
the test is aborted with message:
"The mock function has no default action set, and its return type has
no default value set."


Could you be of any help?

--vdevarap


On Jul 8, 4:58 pm, Amanda Robinson <amandarobin...@google.com> wrote:
> If you expect no call, then you should test for that and use StrictMock.
>
> If you don't care, you still need to set a default response:
> ON_CALL(MockClass, DoSomething(int))
>   .WillRepeatedly(Return(0));
>

Vlad Losev

unread,
Jul 8, 2010, 5:47:44 PM7/8/10
to Google C++ Mocking Framework
Hi Vdevarap,

Because iface_b is not a built-in type, Google Mock does not know what its default value is. You need to supply a default value yourself. See http://code.google.com/p/googlemock/wiki/CookBook#Setting_the_Default_Value_for_a_Return_Type for details.

--vdevarap


On Jul 8, 4:58 pm, Amanda Robinson <amandarobin...@google.com> wrote:
> If you expect no call, then you should test for that and use StrictMock.
>
> If you don't care, you still need to set a default response:
> ON_CALL(MockClass, DoSomething(int))
>   .WillRepeatedly(Return(0));
>
> On Thu, Jul 8, 2010 at 12:59 PM, vdevarap <dvgo...@gmail.com> wrote:
>
> > Hi,
>
> > I am using G mock and g test to unit test my code.
>
> > I did not expect a call to one of the mock methods. so i used Nice
> > Mock to avoid the warning.
> > still the test is failing with the below error:
>
> > : Failure
>
> >    The mock function has no default action set, and its return type
> > has no default value set.
> > Aborted
>
> > any help is appreciated.
>
> > Regards,
> > vdevarap
>
>

-- Vlad

Amanda Robinson

unread,
Jul 8, 2010, 6:22:42 PM7/8/10
to vdevarap, Google C++ Mocking Framework
Check out the doc on Actions, as that's what it's expecting:

For any mocked method that returns something, you need to tell it what to return. Here it looks like you want to return an iface_b (either a real or mocked one).

On Thu, Jul 8, 2010 at 2:42 PM, vdevarap <dvg...@gmail.com> wrote:

vdevarap

unread,
Jul 9, 2010, 12:25:19 PM7/9/10
to Google C++ Mocking Framework

Hi Amanda and Vlad,

Thanks for your replies. I understand now. what you suggested is
right. i have to return a reference to the mock object of iface_b. i
was not using proper action.

Returning Reference from mock methods section solved my problem.
http://code.google.com/p/googlemock/wiki/CookBook#Using_Actions


Thanks,
Vdevarap



On Jul 8, 6:22 pm, Amanda Robinson <amandarobin...@google.com> wrote:
> Check out the doc on Actions, as that's what it's expecting:http://code.google.com/p/googlemock/wiki/ForDummies#Actions:_What_Sho...
> ?
>
> For any mocked method that returns something, you need to tell it what to
> return. Here it looks like you want to return an iface_b (either a real or
> mocked one).
>
Reply all
Reply to author
Forward
0 new messages