I always dreaded learning about Mocking in testing assuming it would be something too complex, and I never really had the requirement of using Mocking in my Laravel tests. We create provisions in our tests so that the Fake / Mock classes behave as the Original class and since we are not dealing with the original class, we can also leverage extra liberty of manipulating and playing around with the Fake class.

In our test, we want to test the execute method of the controller and assert it's return value, but we don't to call the perform method of the Context class.

In our tests, to mock the Context class, we can make use of mock method that is provided by Laravel's base test case class.

Before executing the execute method of the MyController class, the mock method prepares a mock object of the Context class and replaces it with the original wherever it appears in the execution.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners