Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mocking laravel db facade on when method #1151

Open
besingamkb opened this issue Nov 25, 2021 · 1 comment
Open

mocking laravel db facade on when method #1151

besingamkb opened this issue Nov 25, 2021 · 1 comment

Comments

@besingamkb
Copy link

Hi,

I am using laravel with mockery to unit test my queries.. how can I mock the when method of laravel db query using mockery? so here is what I want to test..

<?php
// ... TestRepo.php
class TestRepo
{
    public function testQb()
    {
        DB::table('users')
            ->when(true, function($query) {
                $query->where('email_verified_at', null);
            })
            ->get();
    }
}

and here is what my test look like..

/**
 * A basic test example.
 *
 * @return void
 */
public function test_example()
{
    DB::shouldReceive('table')->once()->with('users')->andReturnSelf();
    DB::shouldReceive('get')->once()->andReturn(collect(new User()));
    (new TestRepo())->testQb();
    $this->assertTrue(true);
}

the only missing on this test is the part where the when method is being called. is this possible to mock also on mockery? and how can I do that? thanks in advance

@davedevelopment
Copy link
Collaborator

Not overly familiar with Laravel's facade/mockery support, so not entirely sure if I'm honest, but you could try adding:

    DB::shouldReceive('when')->andReturnSelf();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants