Support the ongoing development of Laravel.io →
Session Testing
Last updated 1 year ago.
0

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

Last updated 1 year ago.
0

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: http://laravel.com/docs/facades#facade-class-reference.

Here is a better overview of this topic than I can provide: http://taylorotwell.com/response-dont-use-facades/

Last updated 1 year ago.
0

See http://laravel.com/docs/testing#mocking-facades 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 1 year ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Moderators

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

Your logo here?

Laravel.io

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

© 2024 Laravel.io - All rights reserved.