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.
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.
Brain Monkey’s Hook Toolset
The Problem – Why We Need to Simulate WordPress
Testing Hooks with Brain Monkey
Testing a Action Fired
Testing a Filter Has a Callback
Testing a Callback is Registered to a Filter Hook
`andAlsoExpectIt()`
Let’s refactor our test and use the andAlsoExpectIt() method. You’ll also learn about Mockery’s ordered() method for declare the expectations should run in a specific order.
`expect()` – Behavior Options
There are multiple behavior (task) options you can use for the expect() patching function: andReturn(), andReturnValues(), andReturnNull(), andReturnUsing(), and andThrow(). Let’s walk through each one and write some code to see how to use them in your testing suite.
`expect()`
Let’s dig into the expect() testing function in Brain Monkey. This function lets you redefine the expected behavior of a function that you want to mock out and make it part of the tests assertions.