[英]How to test Laravel Scout (with Algolia)?
我有一段這樣的代碼:
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;
}
問題是,我該如何編寫測試? 如果我只是嘗試在測試中提供“名稱”,它將不會返回任何內容,例如在測試過程中search()函數根本無法正常工作。 真的很難找到任何東西,但是信息很稀少,我最終只能做類似“將Algolia驅動程序設置為null”之類的事情,但是我設法做到了,但是由於我不知道該怎么做,所以沒有任何效果。這樣做以及如何在測試中應用它。 絕對沒有成功測試的例子,只有幾個簡短回答的問題並沒有太大幫助。
一項測試:
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();
...
}
因此,我最終得到的是空的responseContent,這意味着這不是測試此方法的正確方法。 有什么想法嗎?
為什么不僅僅測試您已經正確配置了類以使用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
}
確保在測試環境中設置禁用的Laravel Scout。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.