Using Trompeloeil, a mocking framework for modern C++
summary
Trompeloeil is a C++14, header-only mocking framework (single header ~3.5k lines) that stays within the language and integrates via small adapters to existing test frameworks; examples use a templated order/whisky-store to show single-header ~3.5k LOC, explicit return requirements (no hidden defaults) and everything forbidden by default so tests fail loudly. Features demonstrated include sequences for ordering, named expectations via unique_ptr, custom matchers and printing, callback capture, and required destruction (death‑watch) to verify object lifetime; the example suite is implemented and made green after fixing bugs.
AI-written summary · about
chapters (12)
- 0:00Start
- 8:09Mocks and Stubs
- 17:58Compilation Errors
- 19:40Create a Sequence Object
- 27:43Anatomy of Tests
- 33:31Print Function
- 59:13Lifetime of Expectations
- 1:06:11Since I'M Focusing on the on the Error Message Here and Not on the Predicate I'Ll Just Make a Silly Nonsense Predicate the System Does Much Sorry and I Write the Error Message Function So Hey When this Function Is Called Your the Error Message Has Already Started To Be Constructed so the Failing Value Has Already Been Printed so the Job of this Function Is To Explain Why Was this Not Fulfilled What Why Are We Are Happy with this Value so It Already Says Something like Expected Underscore One in Blah Blah and Then I Called Compare Print on the Container
- 1:08:04So Running this We See Now Highlighted in Yellow the Error Message and I Think that's Okay so since this One Is Good We Update the Change Updated Test To Ensure that this Is Indeed in Your a Message by Checking It out of Reg Axis the Reason I Choose a Regular Expression Is because before these Things That Were File Online and You Really Don't Want To Match those by Exact String Comparison Info Is another Catch Thing but It's It's a Message That Is Printed if the Test Fails Not Otherwise so It Gives a Better Visual Feedback in Case this Becomes Broken Running this It Tests It Pauses So There Is Good So another Test Record with the Article and Quantity in Stock Is Accepted and that Is a Short One because It Really Shouldn't Fail so It Shouldn't Throw So Everything Should Be like Nothing since We Have a Fake Predicate That Always Stands We Get the Message but that Is Expected
- 1:09:21So Let's Update the the Predicate Say that if You Find that the Article Has a Key in in the Map Lands Okay and this Works and One Final Test To Ensure that You Get the Limit Okay so We Don't Have an Off by One To Say that When We Ask for 21 and Almonds It Should Fail So Again Update the Predicate In To Check that It's Not Enough that It Gets a Key but the Quantity Must Be Sufficient to and Now Everything Works and Now We Can Update Our Stock for the Reserve
- 1:13:32This and They'Ve Been Annoying Actually but It's Not a Big Deal because either Other You'Re Mocking via a Pure Abstract Based Class and Then You Clean It Really Really Should Have another Virtual Destructor Anyway or in this Case It's Tempted I'M Working with a Concrete Class and Then It's Just Something in the in the Mock the Real Thing Doesn't Have To Have It It's that It's Important so with the Exchange Compiling Works and Running It Gives Error Message that Store Is Still Alive Okay So Now We Know that the Test Can Detect the Fader That Is Excellent and We Have Our Field Function Here We Just Go with It and We Long It and We'Re Home Good There Is One Thing though Strictly Speaking as I Said Earlier about Requirements on Different Calls Different Different Functions They Are Unrelated There Is no Order between Them and while We Don't Care about Which Order the Whiskeys Are Removed We Really Do Care that They Are all Removed before the Story's Destroyed
- 1:19:46Store in Case a Parameter Is a Pointer That You Can Say that this Matter Applies to the Reference Value of that Pointer Rarely Used but Can Be Convenient and I'Ll Show You the Horror Stories with Death Watch and Required Destruction Didn't Show You Named Which Required Destruction but I Bet You Figured that One Out Too and How To Use Sequences Also on that So from Pro Has a Very Small Api It's these Two Pages Print Them Out Pinned on Topic to the Wall Next to Your Monitor and You Have Everything Questions Oh Nine Minutes Left I Think no Seven and a Half
related talks
53mUsing Trompeloeil - a mocking framework for modern C++
01h 01m“Mocking Frameworks considered harmful”
04mUsing C++14 to mock C functions
06mMocking C++
01h 18mMocking Framworks considered, harmful?!
52mPlease don’t mock me
31mBeyond the Mockery- Why We Should Embrace Testing Without Mocking Frameworks
39mPlease Don't Mock Me
