Q:

Laravel Scout and Elastic Search Don't Work in Tests

I have elastic search working when not in a testing environment. However when I create a model in a test for some reason it does not work. I have tried running it with the queue and without the queue. I can see that the job gets created and that the job runs before making the call. However, I am able to pull things that are already in the database in the test code. Any ideas why it would not work in testing?
This is my test code
$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);

A:

I found that I had to add a slight delay to the get call as the index was not updated before the call was made. So I added sleep(1) and that fixed it. Also, I found that it is best to specify a separate index for the testing environment in the config.
$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);

