Table of contents

Solution 3b: Chainable Mock Methods

Similar to the steps taken from the macro version of the visitable mock object to the one with mock methods you have to replace the macro code with the according method template members.

And in the same manner you must add the forwarder methods. Due to the different working method there is no need to provide an overloaded method for constraints.

class ChainMock : public Interface
                , public ChainableMockObject
{
  public:

    ChainMock()
      : ChainableMockObject("ChainMock", 0)
      , open_mocker("open", this)
      , read_mocker("read", this)
      , write_mocker("write", this)
      , close_mocker("close", this)
      , calculate_mocker("calculate", this)
    {}

    void open(const std::string &filename)
    {
      open_mocker.forward(filename);
    }

    ChainableMockMethod<void, std::string>   open_mocker;
    ChainableMockMethod<std::string>         read_mocker;
    ChainableMockMethod<void, std::string>   write_mocker;
    ChainableMockMethod<void>                close_mocker;
    ChainableMockMethod<unsigned, unsigned>  calculate_mocker;
};

Now create the mock object and a reference to the mock method you intend to use.

    ChainMock mock;
    ChainableMockMethod<std::string> &reader(mock.read_mocker);

The only action left is to set up the behaviour. This is done with exactly the same method invocations as in the example with the macros.

    reader.stubs()
          .will(new ReturnStub<std::string>("record-1"));

    reader.stubs()
          .will(onConsecutiveCalls(new ReturnStub<std::string>("record-1"),
                                   new ReturnStub<std::string>("record-2"),
                                   new ReturnStub<std::string>("record-3")));

chainmock2.cpp contains the complete source code.

Next: Solution 4: Poor Man's Mock Objects

Table of contents

 All Classes Namespaces Files Functions Variables Typedefs Friends Defines

Generated on Tue Jan 5 18:03:33 2010 for mockpp-tutorial by  doxygen 1.6.1