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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners