douyin7829 2017-10-16 08:58
浏览 66

如何测试Laravel Scout(与Algolia一起)?

I have a piece of code like this:

 public function index(Request $request, Runner $runnerParam)
{
    $name = $request->input('name');
    $fromDate = $request->input('from_date');
    $toDate = $request->input('to_date');

    $runners = Runner::query();

    if ($name) {
        $runners =  $runnerParam::search($name);
    }

    if ($fromDate && $toDate) {
       $runners->where('created_at', '<=',$toDate )
       ->where('created_at', '>=', $fromDate);
    }

    switch ($type) {
        case 1:
            $runners->where('role', '=', runner::PRO);
            break;
        case 2:
            $runners->where('role', '=', runner::AMATEUR);
            break;          
    }

    $runners = $runners->get();

    foreach($runners as $runner){
     $runner->distance = $runner->stats->sum('distance');
    }

    return $runners;    
}

The question is, how do I write test for this? If I just try to provide 'name' in test, it will return nothing like search() function isn't working at all while testing. Tried really hard to find anything on this, but the info is scarce and I only ended up with something like 'set Algolia driver to null', which I managed to do, but to no effect since I don't know what's the point of doing so and how do you apply it in tests. There are absolutely no examples of successful tests out there, just a few questions with short answer that didn't quite help.

A piece of test:

public function testNameFilter()
{

    $this->logIn();

    $runners = factory(runner::class, 30)->create();

    $name = $runners[0]->name;


    $response = $this->json('get', route('api::runners.get'), ['name' => $name]);

    $responseContent = $response->getContent();

    ...
}

So, what I get in the end is empty responseContent, which means this is not the right way to test this. Any thoughts?

  • 写回答

1条回答 默认 最新

  • du131642 2017-11-28 01:37
    关注

    Why not just test that you've properly configured your class to use Laravel Scout, vs. testing that Laravel Scout works as expected?

    public function class_uses_scout() 
    {
        $this->assertTrue(in_array('Laravel\Scout\Searchable', class_uses('App\FooModel')));
    }
    
    public function class_has_searchable_array()
    {
        // compare the searchable array with a hardcoded array here
    }
    

    Be sure to set your disable Laravel Scout in your test environment.

    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大