mocks expecting no calls

400 views
Skip to first unread message

Bryce Fricke

unread,
Sep 3, 2013, 10:43:30 PM9/3/13
to cppu...@googlegroups.com
I looked through the documentation and searched a bit on the group here and didn't find anything, so I'm hoping this isn't a useless question.

I'm looking for a clean way to set up a test which will fail only if a specific mock function is called.  I don't really care what other functions get called, so long as one specific call is not made.  What I've been doing is setting things up to expect 1 call, and then faking that call so that the test will fail if the mock function is called again.

mock().expectOneCall("MyFunction");
mock().actualCall("MyFunction");
mock().ignoreOtherCalls();

This has the desired outcome, but feels a little hacky.  Any better way to go about this?  I think the ideal would probably be something along the lines of
mock().expectNoCalls("MyFunction");
mock().ignoreOtherCalls();

 Is this completely stupid?  If this is something that doesn't exist and I were to fork the repo and implement it, is it something that might potentially be able to get pulled into the mainline (assuming I didn't do a crap job of course)?

Thanks

-Bryce

Ryan Hartlage

unread,
Sep 3, 2013, 10:54:59 PM9/3/13
to cppu...@googlegroups.com
Have you tried taking advantage of mock scope?  You might be able to globally disable mock and then enable just the scope you're interested in.  Read about it here: http://cpputest.github.io/mocking_manual.html#mock_scope

Ryan


--
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.

A. Robert S.

unread,
Sep 4, 2013, 12:25:19 PM9/4/13
to cppu...@googlegroups.com
I might be wrong, but I believe that, since you are using mock().ignoreOtherCalls(), you pretty much have to do it the way you outlined above (or else, everything would be ignored, including the call you don't want to occur).
 
Depending on the part of your test that is not shown, mock().disable() and mock().enable() may be helpful. e.g.:
 
  mock().disable()
  do_some_setup_here_that_might_call_mocks
  mock().enable()
  call_function_to_be_tested_here
 
Actually, I believe yours is a pretty good solution to a common problem.
 
Also, if the call that must not occur happens to be some sort of assert(), you might write tests to provoke the assert and make sure it did trigger.
 
Robert

Bas Vodde

unread,
Sep 8, 2013, 2:52:29 AM9/8/13
to cppu...@googlegroups.com

Using the mock scope, as suggested earlier, would work pretty well also :)

It allows you to ignore only a subset of calls…

Bas
Reply all
Reply to author
Forward
0 new messages