简体   繁体   English

如何测试Laravel Scout(与Algolia)?

[英]How to test Laravel Scout (with 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. 如果我只是尝试在测试中提供“名称”,它将不会返回任何内容,例如在测试过程中search()函数根本无法正常工作。 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. 真的很难找到任何东西,但是信息很稀少,我最终只能做类似“将Algolia驱动程序设置为null”之类的事情,但是我设法做到了,但是由于我不知道该怎么做,所以没有任何效果。这样做以及如何在测试中应用它。 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. 因此,我最终得到的是空的responseContent,这意味着这不是测试此方法的正确方法。 Any thoughts? 有什么想法吗?

Why not just test that you've properly configured your class to use Laravel Scout, vs. testing that Laravel Scout works as expected? 为什么不仅仅测试您已经正确配置了类以使用Laravel Scout,而是测试Laravel Scout是否按预期工作?

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. 确保在测试环境中设置禁用的Laravel Scout。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM