使用Laravel Scout索引的模型进行测试失败

时间:2017-06-30 14:21:53

标签: php laravel phpunit laravel-scout

我正在编写一个用Scout查找模型的测试。我在Laravel 5.4上并使用提供程序"tamayo/laravel-scout-elastic": "^3.0"

似乎在我的测试中,当我开始搜索模型时,索引创建的项目没有完成。这是真的?我怎样才能解决这个问题?我的队列已设置为syncSCOUT_QUEUE设置为false

以下是一个保持失败的测试示例(无法断言搜索结果包含给定的帖子)。非常感谢任何帮助。

<?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.');
    }
}

0 个答案:

没有答案