【发布时间】:2019-03-18 16:51:29
【问题描述】:
我的Laravel 应用程序上有以下控制器:
class ProjectController extends Controller {
...
public function index() {
$projects = Project::where('is_completed', false)
->orderBy('created_at', 'desc')
->withCount(['tasks' => function ($query) {
$query->where('is_completed', false);
}])->get();
return response()->json($projects);
}
...
}
被以下路由引用:
Route::get('projects', 'ProjectController@index');
当我转到网址时:http://localhost:8000/api/projects 我得到:
[
{
"id": 2,
"name": "Project 02 Title",
"description": "Project 02 Description",
"is_completed": 0,
"created_at": "2018-10-13 16:23:28",
"updated_at": "2018-10-13 16:23:28",
"tasks_count": 0
},
{
"id": 1,
"name": "Project 01 Title",
"description": "Project 01 Description",
"is_completed": 0,
"created_at": "2018-10-13 16:22:52",
"updated_at": "2018-10-13 16:22:52",
"tasks_count": 0
}
]
另外,我有以下测试文件:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Foundation\Testing\RefreshDatabase;
class ProjectTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testExample()
{
$response = $this->get('/');
// this is not working:
// $response->assertJson([ 0 => [ 'name' => 'Project 02 Title' ] ]);
}
}
?>
我想要对testExample() 做的是检查输出数组(JSON 格式)上第一个元素的name 成员是否具有字符串值:Project 02 Title。
知道如何实现这一目标吗?
谢谢!
【问题讨论】:
标签: php laravel eloquent phpunit laravel-5.7