Chapter 2. Frequently And Never Asked Questions

2.1. I have the following simple method but it does not compile.
2.2. How should one organise production code and test code?
2.3. Where can I ask a question regarding mockpp?
2.1.

I have the following simple method but it does not compile.


     const X& method(int p1);
    

Since mockpp emulates all method calls there is no variable to which you can return a reference. One solution is, to use a shadow method. The trick is to separate the mocking elements and the method in question which contains a real variable.


     MOCKPP_VISITABLE1(ClassName, shadow_method, int);

     X shadow_variable;  // the variable is a class member

     const X& method(int p1)
     {
        shadow_variable = shadow_method(p1);
        return shadow;
     }
    

2.2.

How should one organise production code and test code?

There is no general solution how to organise the sources to both run the tests and create a releasable binary without debugging and test code.

A flexible and clean approach is the one used within mockpp:

  • Place all the classes and funcions of your production code into appropriate libraries. All the source code for the according library is in a separate subdirectory.
  • Create a minimalistic application source file with your main() in a top-level directory. This application uses the former libraries.
  • Create a subdirectory tests within each sub-project which contains all the test files needed. Depending on your test framework the test files form a libary or an executable. Upon execution the above libray with the production code is linked dynamically.

So the layout of your project tree might look like this:


       projectroot
                 \_ main()
                 |
                 \_ subproject1
                 |             \_ tests
                 |
                 \_ subproject2
                               \_ tests

     

2.3.

Where can I ask a question regarding mockpp?

Send a mail to the according mailing list or point your browser to the forum.