Laravel Scout和弹性搜索在测试中不起作用

时间:2016-12-20 01:46:36

标签: php laravel elasticsearch

我没有在测试环境中进行弹性搜索。但是,当我因某种原因在测试中创建模型时,它不起作用。我已经尝试使用队列运行它而没有队列。我可以看到在创建调用之前创建了作业并且作业已运行。但是,我能够在测试代码中提取数据库中已有的东西。有什么想法它不能用于测试吗?

这是我的测试代码

$user    = $this->newLoggedInUser();
$invoice = factory(App\Invoice::class)->create(['account_id' => $user->account_id]);


$this->get($this->url() . '?q=' . $invoice->title, $this->userAuthHeader($user))
        ->seeJson([
            'Title'       => 'Invoice: ' . $invoice->number . ($invoice->title ? ' - ' . $invoice->title : ''),
            'Description' => 'Customer: ' . $invoice->customer->name,
            'Type'        => 'Invoice',
        ])
        ->assertResponseStatus(200);

1 个答案:

答案 0 :(得分:-1)

我发现我必须在get调用中添加一点延迟,因为在调用之前索引没有更新。所以我添加了sleep(1)并修复了它。另外,我发现最好在配置中为测试环境指定一个单独的索引。

$user    = $this->newLoggedInUser();
$invoice = factory(App\Invoice::class)->create(['account_id' => $user->account_id]);

sleep(1);

$this->get($this->url() . '?q=' . $invoice->title, $this->userAuthHeader($user))
        ->seeJson([
            'Title'       => 'Invoice: ' . $invoice->number . ($invoice->title ? ' - ' . $invoice->title : ''),
            'Description' => 'Customer: ' . $invoice->customer->name,
            'Type'        => 'Invoice',
        ])
        ->assertResponseStatus(200);