Stan
Stan

Reputation: 491

Test with models indexed by Laravel Scout fails

I'm writing a test for finding models with Scout. I'm on Laravel 5.4 and use the provider "tamayo/laravel-scout-elastic": "^3.0".

It seems that in my tests indexing the created items isn't completed when I start searching for a model. Is this true? How can I fix this? My queue is already set to sync and SCOUT_QUEUE is set to false.

Here is an example of a test that keeps failing (Failed asserting that search results contain the given post). Any help is greatly appreciated.

<?php

namespace Tests\Unit;

use App\Models\Category;
use App\Models\Post;
use App\Models\User;
use Tests\TestCase;

class SearchTest extends TestCase
{
    /** @test * */
    public function it_searches_the_whole_category_tree_for_posts()
    {
        // Given
        /** @var Category $parentCategory */
        $parentCategory = \factory(Category::class)->create([
            'title' => 'myParentCategory',
        ]);
        /** @var Category $childCategory */
        $childCategory = \factory(Category::class)->create();
        $childCategory->makeChildOf($parentCategory);
        /** @var Post $post */
        $post = \factory(Post::class)->create([
            'user_id' => \factory(User::class)->create()->id,
        ]);
        $post->requestCategories()->attach($childCategory);

        // When
        $searchResults = Post::search('myParentCategory')->get();

        // Then
        $this->assertTrue($searchResults->contains($post), 'Failed asserting that search results contain the given post.');
    }
}

Upvotes: 4

Views: 1349

Answers (1)

zmk110
zmk110

Reputation: 902

What exactly are you testing here? Are you just testing that ::search('foo') returns the desired results? If so then your actually testing that Laravel Scout functions as expected, which isn't, and shouldn't be your/our job.

At most you can test that you've properly configured your model(s) to use Laravel Scout as expected.

If a simple/silly test would suffice, then the below code should help;

namespace Tests\Unit;

use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;

class ScoutInstallTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Verify the class uses Laravel Scout
     *
     * @group scout-install
     * @test
     */
    public function foo_model_uses_scout()
    {
        $this->assertTrue(in_array('Laravel\Scout\Searchable', class_uses('App\FooModel')));
    }

    /**
     * Verify that a searchable array does exists, and contains
     * the values we desire to search on.
     *
     * @group scout-install
     * @test
     */
    public function foo_model_has_valid_searchable_array()
    {
        $fooModel = factory(\App\FooModel::class)->create();

        $this->assertTrue([
            'title', // an array of keys that are being indexed.
        ] === array_keys($fooModel->toSearchableArray()));
    }
}

Note disable Laravel Scout in your testing environment; <env name="SCOUT_DRIVER" value="null"/>

Upvotes: 3

Related Questions