You could inject the Session class into your class and than inject a mock instead when testing.
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/
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.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community