We can test that the arguments the mocked function receives are what we expect by using the with() method. Mockery, which is baked into Brain Monkey, inspects the received arguments and then tests each one against what you’ve defined it should receive. This feature allows us to ensure our code is properly doing its work and sending the right set of data to the function we are mocking. Here, let me show you.
Labs
Labs are hands-on coding projects that you build along with Tonya as she explains the code, concepts, and thought processes behind it. You can use the labs to further your code knowledge or to use right in your projects. Each lab ties into the Docx to ensure you have the information you need.
Each lab is designed to further your understanding and mastery of code. You learn more about how to think about its construction, quality, maintainability, programmatic and logical thought, and problem-solving. While you may be building a specific thing, Tonya presents the why of it to make it adaptable far beyond that specific implementation, thereby giving you the means to make it your own, in any context.
`stubs()`
Stubs give you a way to redefine multiple functions by passing an array to Brain Monkey. This toolset is handy for things such as WordPress escaping and internationalization functions, as we can bulk define these functions in your Test Case. Here is an example of how we do it with the Beans framework.
`when()` – Code Challenge
Time for a code challenge to make sure you now understand how when() works and how to use each of its different task options. Let’s write 4 different when() definitions for the WordPress function esc_html_e(). Then we’ll discuss how to select the best option as well as how to keep your code DRY.
Testing an Action Has a Callback
Testing a Callback is Registered to an Action Hook
Testing a Filter Fired
Brain Monkey’s Hook Toolset
The Problem – Why We Need to Simulate WordPress
Testing Hooks with Brain Monkey
Our themes and plugins use hooks to run code when an event fires. We use WordPress’ functions to add or remove a hooked callback, fire the event, or check the status. How do you unit test those interactions without loading WordPress? Meet Brain Monkey. Brain Monkey gives you the power to unit test hooks by simulating WordPress. In this lab, you’ll walk through building unit tests for hooks.