测试 CursorPaginator 时出现异常
Exception when testing CursorPaginator
我是 运行 以下代码仅在测试中失败,但在浏览器中运行正常的问题。
代码应为 Eloquent 集合提供 CursorPaginator
并 return 为 JSON。
测试代码:
public function testMoreNotesAreReturnedIfRequested()
{
$item = Item::factory()->has(Entry::factory()->for($this->user)->count(6))->create();
// Get page one of paginator, and request page 2 using the URL it returns
$response = $this->actingAs($this->user)->json('get',"/items/{$item->id}/notes/more");
$next = $this->actingas($this->user)->json('get',$response->decodeResponseJson()['next_page_url']);
$next->assertJson(fn (AssertableJson $json) => $json
->has('data')
->has('paginator'));
}
以上失败,出现以下异常:
Only arrays and objects are supported when cursor paginating items.
我已经确认在第 1 行中创建了 6 个条目,并且它们都具有不同的毫秒创建时间,如谷歌搜索问题时所建议的那样。
有什么想法吗?
控制器代码:
return $item->entries()
->latest()
->with(['content','user'])
->cursorPaginate(5)
->withPath("/items/$item->id/notes/more")
->through( static function ($item) use ($request) {
$item->setAttribute('can_edit',$request->user()->can('update',$item->content));
$item->setAttribute('can_delete',$request->user()->can('delete',$item->content));
return $item;
});
编辑 - 每次都测试失败,而浏览器仅在集合中的项目数为 6、7、16 或 17 时失败。我现在更困惑。
我是 运行 以下代码仅在测试中失败,但在浏览器中运行正常的问题。
代码应为 Eloquent 集合提供 CursorPaginator
并 return 为 JSON。
测试代码:
public function testMoreNotesAreReturnedIfRequested()
{
$item = Item::factory()->has(Entry::factory()->for($this->user)->count(6))->create();
// Get page one of paginator, and request page 2 using the URL it returns
$response = $this->actingAs($this->user)->json('get',"/items/{$item->id}/notes/more");
$next = $this->actingas($this->user)->json('get',$response->decodeResponseJson()['next_page_url']);
$next->assertJson(fn (AssertableJson $json) => $json
->has('data')
->has('paginator'));
}
以上失败,出现以下异常:
Only arrays and objects are supported when cursor paginating items.
我已经确认在第 1 行中创建了 6 个条目,并且它们都具有不同的毫秒创建时间,如谷歌搜索问题时所建议的那样。
有什么想法吗?
控制器代码:
return $item->entries()
->latest()
->with(['content','user'])
->cursorPaginate(5)
->withPath("/items/$item->id/notes/more")
->through( static function ($item) use ($request) {
$item->setAttribute('can_edit',$request->user()->can('update',$item->content));
$item->setAttribute('can_delete',$request->user()->can('delete',$item->content));
return $item;
});
编辑 - 每次都测试失败,而浏览器仅在集合中的项目数为 6、7、16 或 17 时失败。我现在更困惑。