Implementing Integration with a Mocking Framework

59 views
Skip to first unread message

Ovanes Markarian

unread,
Sep 23, 2017, 2:42:48 PM9/23/17
to CATCH
Hi *,

I'd like to port Turtle Mocking (https://github.com/mat007/turtle) framework to support Catch. May be someone can give me insights into the Catch internals.

Turtle was developed to support Boost Test at first. What Boost.Test has that I can't find in Catch is setting a checkpoint. Actually I see the ScopedMessage class which offers similar functionality, but it won't work here. To integrate Turtle with some (Unit) Testing framework one needs to implement an adapter with the following members:


template<typename Result>
struct catch_turtle_mock_adapter
{
[[noreturn]] static Result abort();


template<typename Context>
static void fail( const char* message
, const Context& context
, const char* file = "file://unknown-location"
, int line=0
)
;

template<typename Context>
static void call(const Context& context, const char* file, int line);

static void pass(const char* file, int line);
};


The difficulty I am 

So, how is the adapter is supposed to work from the Turtle's point of view. Suppose there is a mock object : `foo` with a method `void bar(char const*)`. Typical test case looks like:

{
  object_owning_mock owner_of_mock;

  MOCK_EXPECT(owner_of_mock.foo.bar).once().with("abc");   // 1 => call catch_turtle_mock_adapter::pass(...)

  owner_of_mock.ask_bar("abc");   // 2  => call catch_turtle_mock_adapter::call(...)
}  // 3 => scope exit: verify expectations => if failures call: catch_turtle_mock_adapter::fail(...)


Boost implementation calls Boost Frameworks set_checkpoint function, e.g.:

 boost::unit_test::unit_test_log.set_checkpoint( file,
                static_cast< std::size_t >( line ) );


Is there a similar mechanism to set a checkpoint in Catch? I looked into the INFO, etc. macro expansions and they all work with ScopedMessage, which does not work in that case, as the ScopedMessage is going to be destroyed when exiting for example the `pass` function, but the test case failure happens when `mock::verify(...)` is called or scope exit of the TC occurs. I saw that ScopedMessage creates a MessageInfo instance and pushes it on Catch stack, but will it be automatically reclaimed, if TC evaluation ends? I am a bit unsure how to handle such cases:

SCENARIO("...")
{
   WHEN("...")
   {
      THEN("...")
      {
         // use some mocks here

        AND_THEN("...")
        {
           // use some more mocks here
        }  // verify part of the mocks were called and remove their pass points
      } // verify the remaining part of the mocks were called and remove their pass points...
   }
}

It works with Boost.Test, because such a nesting is not (part of/possible with/used with) Boost.Test.



Thanks for any helps and suggestions,
Ovanes











Dvir Yitzchaki

unread,
Sep 23, 2017, 4:57:39 PM9/23/17
to Ovanes Markarian, CATCH
Maybe you can take inspiration from another mocking framework which does support catch - https://github.com/eranpeer/FakeIt

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

Ovanes Markaryan

unread,
Sep 23, 2017, 6:27:09 PM9/23/17
to Dvir Yitzchaki, CATCH


On 23. Sep 2017, at 22:57, Dvir Yitzchaki <dvi...@gmail.com> wrote:

Maybe you can take inspiration from another mocking framework which does support catch - https://github.com/eranpeer/FakeIt

Thanks, I already had a look at it. They have different approach as far as I understand and react on error events. Turtle works differently as far as I understand.
Reply all
Reply to author
Forward
0 new messages