Summer Sale! All accounts are 50% off this week.

untymage's avatar

Cannot Mock class with parameters in __construct

namespace App;

class Episode
{

    public function __construct(public int $id)
    {

    }

    public function save()
    {
        dump('here');
    }
}

TestCase.php:

    /** @test */
    public function test_save_method()
    {
        $this->mock(Episode::class, function (MockInterface $m){
            $m->shouldReceive('save')->andReturnTrue();
        });

        app(Episode::class)->save(); //No issue, save() method hicjacked as excepted

	    // But as soon i pass arguments:

		app(Episode::class, ['id' => 2])->save(); //save method called an didnt mocked!
}

I want to mock Episode class, But the problem is when i pass arguments to the class the laravel container doesnt pass the mock object and instead call the real method

I describe in comments, Can anyone tell why is not pass mock object ?

0 likes
1 reply

Please or to participate in this conversation.