I don't understand ReturnThruPtr mock function

588 views
Skip to first unread message

מקסים סקופין

unread,
Mar 29, 2022, 3:33:45 PM3/29/22
to throwth...@googlegroups.com
Hello, Mark, and anybody may to help me !

In this time I learn Iteration 3 and should make Homework 1. But I don't understand ExpectAnyArgAndReturn and ReturnThruPtr CMock functions.

It's right, that ExpectAnyArgAndReturn return STATUS_OK or other value from function into source code without "sending" arguments to ?

And it's right, that ReturnThruPtr "send" pointer from test to source code ?

There CMock functions are works "into pair" ? I can't to use ReturnThruPtr without ExpectAnyArg function ?

With my respect,

Maxim


Mark Vander Voord

unread,
Mar 31, 2022, 7:51:31 AM3/31/22
to ThrowTheSwitch Forums
Hi Maxim:

Your statements are all correct.

Remember that when using a mock, instead of the real version of the function being called, we call our fake version. The fake version can keep track of a number of things. It can track how many times the function is called and what arguments it was called with (_Expect). It can queue up return values for each time the function gets called, so that we can control what is returned from it (_ExpectAndReturn). If instead, we only care that the function was called but don't want to verify the individual arguments, we can use _ExpectAnyArgs and _ExpectAnyArgsAndReturn respectively.

Similar to the return values, we can queue argument values that we want to be filled in when there are pointer arguments (_ReturnThruPtr). Each time we call this function, it defines the value to be returned on the queue. Here's the important part though: To do that, it needs to know you expected a call to happen first... Therefore before each _ReturnThruPtr, you need one of these: _Expect, _ExpectAndReturn, _ExpectAnyArgs, or _ExpectAnyArgsAndReturn.

The _ExpectAnyArgs* options are frequently used with _ReturnThruPtr because when we want to return a value, we often do not care what the value was beforehand. It's often an empty buffer or whatnot.

Does that make sense?

Mark


--
You received this message because you are subscribed to the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this group and stop receiving emails from it, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/CAMPrmQpZFfuHSmVABG6vDxwfDdAd7J6AZwnyQ%3DbA66MdC-7v-w%40mail.gmail.com.

Maxim Skopin

unread,
Mar 31, 2022, 1:08:11 PM3/31/22
to ThrowTheSwitch Forums
Thank you, Mark, for your explanation.

Maxim

ב-יום חמישי, 31 במרץ 2022 בשעה 14:51:31 UTC+3, mvandervoord כתב/ה:

Maxim Skopin

unread,
Mar 31, 2022, 2:50:22 PM3/31/22
to ThrowTheSwitch Forums
But one moment for better understanding. 

 ?By ReturnThruPtr- function we inserting our pointer INTO the SOURCE CODE STRING we made ExpectAnyArgAndReturn-function for this? Or for NEXT CODE STRING

For example. We have into source code string

if(CommandHardware_CheckForMsg(&Msg)== STATUS_OK ){

For this code string we make in our test next fake :

CommandHardware_CheckForMsg_ExpectAnyArgsAndReturn( STATUS_OK ),

 Its OK, we want to return STATUS_OK. After this we make 

CommandHardware_CheckForMsg_ReturnThruPtr_msg( &msg ).

? The pointer msg is going to CommandHardware_CheckForMsg(&Msg) Msg- argument ? Or, for NEXT using of Msg into our source code 

Thank you for your answer and with my respect,

Maxim
ב-יום חמישי, 31 במרץ 2022 בשעה 20:08:11 UTC+3, Maxim Skopin כתב/ה:

Mark Vander Voord

unread,
Mar 31, 2022, 11:06:12 PM3/31/22
to throwth...@googlegroups.com
Maxim:

The first time CommandHardware_CheckForMsg is called AFTER the Expect and ReturnThruPtr, then msg will be assigned to the return value.

For example, let's say the function I want to test looks like this:

int TimesTen()
{
    int number = 0;
    if (GetNumber(&number) == 0)
        return 10 * number;
    else
        return 0;
}

I am going to create a mock of this function to use it:

int GetNumber(int* number);

One of the tests for TimesTen might look like this:

void test_TimesTen_SHOULD_ReadAValueAndMultiplyItByTen(void)
{
    // I don't care what the starting number is, I just want it to return a status of 0 and a number of 45 for this test
    GetNumber_ExpectAndArgsAndReturn(0);
    GetNumber_ReturnThruPtr_number(45);

    TEST_ASSERT_EQUAL_INT(450, TimesTen() );
}


Maxim Skopin

unread,
Apr 1, 2022, 1:33:56 PM4/1/22
to ThrowTheSwitch Forums
: Thank you, Mark, for your explanation. It's, probably, depends from style of programming. I would do it other 

}int GetNumber(void)
;return number
{

and

(()if(!(number=GetNumber
;return 10*number
else
;return 0

ב-יום שישי, 1 באפריל 2022 בשעה 06:06:12 UTC+3, mvandervoord כתב/ה:

Mark Vander Voord

unread,
Apr 1, 2022, 9:51:15 PM4/1/22
to throwth...@googlegroups.com
Maxim:

I 100% agree that your solution is a better solution for the code. I was only creating an example so you could see how ExpectAnyArgs and ReturnThruPtr are used.

Mark

מקסים סקופין

unread,
Apr 2, 2022, 2:39:02 AM4/2/22
to throwth...@googlegroups.com
Thank you, Mark. At now I understand better. 

With my respect,

Maxim

сб, 2 апр. 2022 г., 04:51 Mark Vander Voord <mvande...@gmail.com>:
Maxim:

I 100% agree that your solution is a better solution for the code. I was only creating an example so you could see how ExpectAnyArgs and ReturnThruPtr are used.

Mark

--
You received this message because you are subscribed to a topic in the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/throwtheswitch/iHlPGMq1Az0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/CAAu8-XF6XqF4Ba%3D9%2BKeQ5UuSQKe5bL6KCTw744%2BmoVqCeB_Y2w%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages