How Mockery Interacts with Laravel Service Container

When testing Laravel applications, you may wish to “mock” certain aspects of your application so they are not actually executed during a given test.

Goal:

How:

Description:

If you have a look at the trait InteractsWithContainer.php you will see this trait is responsible for mocking an instance of an object in the container.

When in your test classes you call mock() method, behind the scene Laravel refers the method in this trait.

As an example, in Laravel Fortify there are some tests, let’s look at one of them as below:

A test case in Laravel/Fortify

Now our question is what actually happens when mock() methods is called?

This mock() method calls the instance() method. Let’s have a look at the it.

Note: Above you see a Mockery instance of the object is what we are passing to the instance() method.

As you see below the instance() method is itself responsible for calling the primary instance() method on app which is the instance of the Laravel IOC Container.

Briefly:

--

--

Posting about Python and Laravel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store