Cvetan's avatar

Laravel scout search with database engine test not returning proper results

I am trying to write test for search which is using Laravel scout and database driver. I have indexed first_name, last_name and email columns. I populate database in setUp test method like this:

protected function setUp(): void
    {
        parent::setUp();

        $password = Hash::make('password');

        // initialize search data
        User::insert([
            [
                'first_name' => 'John',
                'last_name' => 'Doe',
                'email' => '[email protected]',
                'password' => $password
            ],
            [
                'first_name' => 'Jane',
                'last_name' => 'Doe',
                'email' => '[email protected]',
                'password' => $password
            ],
            [
                'first_name' => 'Foo',
                'last_name' => 'Bar',
                'email' => '[email protected]',
                'password' => $password
            ]
        ]);
    }

This is test method:

/**
     * @dataProvider provide_parameters_for_search_test
     */
    public function test_users_can_be_filtered(
        string $query,
        int    $expectedResults
    ): void
    {
        $response = $this->actingAs($this->user)
            ->get(route('users.search', ['query' => $query]));

        $response->assertStatus(200)
            ->assertViewHas('users', function ($users) use ($expectedResults) {
                return count($users) === $expectedResults;
            });
    }

and here are parameters:

public static function provide_parameters_for_search_test(): array
    {
        return [
            ['john', 1],
            ['jane', 1],
            ['foo', 1],
            ['doe', 2],
            ['bar', 1],
            ['example', 2],
            ['acme', 1]
        ];
    }

The test is failing when passing 'example' phrase. I expect two results for these two users that have example emails. Am I doing something wrong?

0 likes
1 reply
LaryAI's avatar
Level 58

It's possible that the test is failing because the search index hasn't been updated yet. Try adding a call to User::searchable() after inserting the data in the setUp() method to ensure that the search index is updated.

Here's an updated setUp() method:

protected function setUp(): void
{
    parent::setUp();

    $password = Hash::make('password');

    // initialize search data
    User::insert([
        [
            'first_name' => 'John',
            'last_name' => 'Doe',
            'email' => '[email protected]',
            'password' => $password
        ],
        [
            'first_name' => 'Jane',
            'last_name' => 'Doe',
            'email' => '[email protected]',
            'password' => $password
        ],
        [
            'first_name' => 'Foo',
            'last_name' => 'Bar',
            'email' => '[email protected]',
            'password' => $password
        ]
    ]);

    User::searchable();
}

This should ensure that the search index is updated before the test runs.

Please or to participate in this conversation.