dougeqiang1619 2016-12-20 01:46
浏览 54
已采纳

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

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);
  • 写回答

1条回答 默认 最新

  • dongwei2610 2016-12-21 04:52
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥15 latex怎么处理论文引理引用参考文献