Menu

#57 Possible documentation improvement suggestion about MOCK_FUNCTION

closed
None
2016-01-03
2015-08-09
Tamás Kiss
No

Hi Mathieu,

Recently I found a presumably correct, but (at least for me) unexpected behaviour when using MOCK_FUNCTION in Turtle 1.2.6. The documentation warns that in case of using features that have an underlying static implementation object (like MOCK_FUNCTION, MOCK_STATIC_METHOD, etc.), verify() and reset() should be called "manually" at each TC to ensure expectation verification, expectation cleanup, and thus the independence of TCs.

This works well if the TC passes, or at least only expected calls are made in it. However, if an unexpected call is caught during the TC, it is aborted immediately. This prevents the execution of the cleanup code at the end of the TC, and the expectations leak into the subsequent TCs causing confusing test results.

Of course once the root cause is clear, using a fixture can quickly correct this problem as the destructor is guaranteed to be executed. I may be the only one who did not expect an immediate abort in the middle of the TC in the above situations, but still, I'd recommend pointing out in the documentation that using a fixture is the only correct way when using any of such mocking features.

It is rather an improvement suggestion than a fault, I just tought it might be useful.

Best Regards,
Tamas

Discussion

  • Tamás Kiss

    Tamás Kiss - 2015-08-09

    Hm, It might also worth mentioning that these use cases not only imply using a fixture, but if any expectations are set to pointers, then those pointers must not point to TC-local automatic variables, because they won't exist by the time the verification is called in the fixture destructor. This is a problem because verify() tries to access the pointed objects when logging the result of the verification.

    Sorry if these are too basic and straightforward things, I only mention them because they might help the next beginner unit tester, like I am right now. :)

     
  • Mathieu Champlon

    • status: open --> accepted
    • assigned_to: Mathieu Champlon
     
  • Mathieu Champlon

    Hi Tamás, and sorry for the delay.

    I believe the correct way to handle this is to verify at the end of the test and use a fixture to reset in case anything leaks out, as you point out.
    This should probably require a bit more explanation in the documentation, I'll try and write a guideline.

    Thanks for your valuable feedback, as always !

    MAT.

     
    • Tamás Kiss

      Tamás Kiss - 2015-10-05

      Hi Mathieu,

      Uhh, this solution is so straightforward that probably that's why I never realized that. Thanks for the tip, and also for the effort to improve the documentation!

      BRs,
      Tamas

       
  • Mathieu Champlon

    Here is an attempt at documenting the pattern : https://github.com/mat007/turtle/pull/18

     
    • Tamás Kiss

      Tamás Kiss - 2015-10-14

      Looks good to me.

       
  • Mathieu Champlon

    • status: accepted --> pending
     
  • Mathieu Champlon

    • status: pending --> closed
     

Log in to post a comment.