Table of Contents – Mocks
The Mocks chapter of The Pester Book has several sections. The final paragraph, of the final page in the final section says, “Mocks are by far the hardest concept in Pester to wrap your head around but are critical in creating good unit tests.” Now, jumping back to the beginning of the chapter, and we have this quote: “Mocks are one of the most important concepts in Pester yet also the least understood.”
Another way to say all this, would have been, “pay attention — it’s about to get good, and if you don’t pay attention, you might as well put the book down.” Well, that didn’t happen for me. Let’s review.
This chapter gives us our first real example of using Mocks. Remember from a few chapters and days back that the first type of test we want to write is a unit test. It’s the smallest test. But, that’s not the most significant part — the most significant part is that it’s a standalone test, that needs all it’s dependencies removed. What’s the mean? This is chapter opens with a walk through where we create a function that reads from a CSV file, and uses the username obtained from the document inside a call to Get-ADUser. If we remove dependence, then we remove this need for a CSV file, and an Active Directory environment. Think about that. Or rather, think about the PowerShell you’ve written in the past. Your testing probably needed all those things. Didn’t it? Yeah…, we know.
You’ll enjoy the opening to this chapter. I did. There’s something exciting about watching a successful test that requires a CSV file and AD, suddenly not need either one.
The chapter closes with a few small sections: Mocking Specific Object Types, Mock Scope, Module-Level Mocks, Parameter Filters, Mock Assertions, and Asserting A Single Mock, just before the chapter’s Wrap Up. Every one of these was important, and vital, to the complete Mocking topic. Read them; they are all very important to fully comprehend!