Support the ongoing development of Laravel.io →
IOC Testing

I'm quite new about testing. I'm wondering what is the best way to make my classe testable. Is it better to do :

class SomeClass {

    protected $dependency;

    public function __construct(Dependency $dependency)
    {
        $this->dependency = $dependency;
    }
    ...

Or use the laravel Ioc Container :

class SomeClass {

    protected $dependency;

    public function __construct($app)
    {
        $this->dependency = $app->make('dependency');
    }
    ...

What do you think ?

Last updated 2 years ago.
0

If you can mock all your dependencies you'll be fine.

Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

marvin-bot marvin-bot Joined 17 Apr 2014

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.

© 2025 Laravel.io - All rights reserved.