Support the ongoing development of →
Session Testing
Last updated 2 years ago.

You could inject the Session class into your class and than inject a mock instead when testing.

Last updated 2 years ago.

matthisstenius said:

To allow for proper unit tests, it is recommended to avoid using Facades.

Not sure where you heard that, but it is incorrect.

You're assuming a Facade is using static methods. In fact, they are just convenient wrapper and behind the scenes the full instances are created for each class accessed through a facade. Testing was one of the primary reasons for doing this. You get the elegant of using a static-like syntax, but still retain the ability to do unit testing. The actual instances are stored in the IoC container. When you call a method on a facade, Laravel resolves the class out of the IoC container and calls the method on a real object.

This is a common mistake when first coming to Laravel. The objects accessed through the Facades are fully mockable and can be properly unit tested.

You can see where all the facades mapped in the IoC container here:

Here is a better overview of this topic than I can provide:

Last updated 2 years ago.

See You can call shouldReceive on the Facade to mock it with Mockery. But you can avoid it by injecting the class, like the example by Taylor linked above.

Last updated 2 years ago.

Sign in to participate in this thread!


Your banner here too?


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.