我正在使用分页测试 API 端点返回的 Laravel 资源
public function test_showing_all_plans(): void
{
$plans = Plan::where('is_active', true)
->paginate(10);
$resource = PlansResource::collection($plans);
$this->withHeaders([
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'AppKey' => 5,
])
->json('GET', '/api/customer/plans')
->assertStatus(200)
->assertExactJson([
$resource->response()->getData(true),
]);
}
所以我的问题是返回的结果不一样,因为端点的路径不等于资源的返回。
这是从端点返回的结果:
"links": {
"first": "http://www.site.local/api/customer/plans?page=1",
"last": "http://www.site.local/api/customer/plans?page=3",
"next": "http://www.site.local/api/customer/plans?page=2",
"prev": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 3,
"path": "http://www.site.local/api/customer/plans",
"per_page": 10,
"to": 10,
"total": 24
}
这是从资源“ $resource->response()->getData(true)
”返回的代码
"links": {
"first": "http://www.site.local?page=1",
"last": "http://www.site.local?page=3",
"next": "http://www.site.local?page=2",
"prev": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 3,
"path": "http://www.site.local",
"per_page": 10,
"to": 10,
}
那么如何将端点传递给我的代码,或者如何使它们相等,是否有正确的方法来测试 Laravel 资源?