Back

Unable to mock database calls


Vitan Ivanov posted 3 months ago

Hello guys,

I am trying to mock the Eloquent Builder calls to the database:

        $user = new User();
        $user->id = 3;
        $collection = new Collection();
        $collection->add($user);

        $mockedBuilder = Mockery::mock(Builder::class);
        $mockedBuilder->shouldReceive('where')
            ->withArgs(['id'=>5])
            ->andReturn($collection);
        App::instance(Builder::class, $mockedBuilder);
        $userAfterMocking = User::where('id',5)->first();
        dd($userAfterMocking->id);

The problem is, instead of calling the mocked builder, the test actually queries the database. Any ideas?


Sign in to participate in this thread!



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