我有一段这样的代码:
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驱动器为空”这样的东西,我设法做到了,但没有效果,因为我不知道有什么意义这样做,你如何在测试中应用它。绝对没有成功测试的例子,只有几个简短的答案没有帮助。
一项测试:
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,这意味着这不是测试它的正确方法。有什么想法吗?