How to mock an internal function of a component using CppUMock.

1,233 views
Skip to first unread message

Venkatesh Krishna mohan

unread,
Apr 25, 2013, 12:27:26 AM4/25/13
to cppu...@googlegroups.com
Hi,
I am a newbie for unit testing,Cpputest and CppUMock. just started with James book on TDD.


Can anyone please help me understand how to mock an internal function of a component. Also please suggest me if am i allowed doing it. Here i illustrate the scenario:


i am testing the class SLedControl and here i test the member function SLedControl::enableSound, this function calls another member function of class SFeedback i.e SFeedback::trigger();


i want to have a mock for the member function SFeedback::trigger();
how do i achieve it??


PS: SFeedback::trigger() has the implementation specific for hardware and my interest here is not to test SFeedback class but the SLedControl class.

Terry Yin

unread,
Apr 25, 2013, 6:08:40 AM4/25/13
to cppu...@googlegroups.com
Hi,

What you are looking for is a "test specific sub-class".

Step 1: Turn SFeedback::trigger() into a virtual function (if it's not a virtual function yet). The cost of doing this will be discussed in the end of this mail.
Step 2: In you test, you create a sub-class of SFeedback, say, SFeedbackForTest, with the function trigger() overrided with a mock implementation.
class SFeedbackForTest : public SFeedback {
     protected:
          void trigger() {
              mock().actualCall("tigger").onObject(this);
          }
 };
Step 3: In you test case, use SFeedbackForTest rather than SFeedback. Then the mock should work.

OK, it's dinner time, manager is calling. I will come back to the virtual function issue later:-) Sorry.

br, Terry



--
You received this message because you are subscribed to the Google Groups "cpputest" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cpputest+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Message has been deleted

Venkatesh Krishna mohan

unread,
Apr 25, 2013, 7:00:41 AM4/25/13
to cppu...@googlegroups.com
Hi Terry,
First of all Thanks for your time in replying.
Second i am sorry.
i forgot to mention that SFeedback::trigger is a static function.

Here is the definition of sfeedback.
class SFeedback
{
public: 
 enum Type {

/* Enum Types*/
}
static void trigger(SFeedback::Type feedbackType);
}

Bas Vodde

unread,
Apr 25, 2013, 7:00:59 AM4/25/13
to cppu...@googlegroups.com

Hi Krishna,

You can make it a virtual function by replacing "static" with "virtual" :)

Bas

On 25 Apr, 2013, at 6:59 PM, Venkatesh Krishna mohan <venk...@gmail.com> wrote:

> Hi Terry,
> First of all Thanks for your time in replying.
> Second i am sorry.
> i forgot to mention that SFeedback::trigger is a static function.
>
> Here is the definition of sfeedback.
> class SFeedback
> {
> enum Type {
>
> /* Enum Types*/
> }
> static void trigger(SFeedback::Type feedbackType);
> }
>
>

Terry Yin

unread,
Apr 25, 2013, 7:32:29 AM4/25/13
to cppu...@googlegroups.com

Hi,
Does the S in SFeedback mean static? Then it's just c code under the c++ cover. I think James has examples in his book about turning function to function pointer and other ways of replacing a function at run time. Those should apply to static functions in class too.

br, terry

Knut Aksel Røysland

unread,
Apr 25, 2013, 4:12:09 PM4/25/13
to cppu...@googlegroups.com
Hi,

There is also the link-time replacement alternative, although the ones already mentioned are usually to be preferred due to flexibility and clarity/understandability.

Link-time replacement means you isolate the definition of the function you want to mock into a source file on its own and compile that into an object file. Then, you create the mock implementation of the same function as an alternative definition in a different source file, compiling that into a different object file. At link-time, you configure your build system to link with the appropriate object file depending on what is being built. When building the test suite executable, the object file containing the mock implementation is used, otherwise the object file containing the regular implementation is used.

Note: Since this is a static member function, it should end up having external linkage, so the above technique should work. If it was a regular static function (internal linkage), the above would not work.

--
Thanks,
Knut Aksel
Reply all
Reply to author
Forward
0 new messages