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.
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.
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.
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.