php - Laravel 用更少的数据测试分页
问题描述
我正在尝试缩短测试时间。目前我有一个测试,我需要测试分页链接和元数据。
在我的控制器中,我的分页设置为 15,但是在我的测试中,我必须使用工厂创建 16 个实例才能断言第 2 页上的数据。
TestGetStudents.php
public function testGetStudents() {
Students::saveMany(factory(Student::class, 16)->make());
$this->get('url/students/list?page=2')
->assertJson([
'meta' [
'current_page' => 2
]
]);
}
学生控制器.php
public function list() {
return Students::paginate();
}
如何编写测试而不必在第二页创建 16 个学生来测试数据?
解决方案
替换您的控制器以接收来自用户的分页参数:
public function list(Request $request) {
$per_page = !($request->input('per_page')) ? 15 : $request->input('per_page');
return Studentes::paginate($per_page);
}
推荐阅读
- r - 将矩阵列表除以矩阵
- java - HttpUrlConnection 错误请求
- javascript - 关于使用 JavaScript 和 DOM 接口遍历 HTML 表格的问题
- bash - 如何杀死单个 GPU 的所有 nvidia 进程
- python - df.apply() 引发 IndexingError: Unalignable boolean Series 作为索引器提供
- reactjs - 如何使用样式组件扩展(MUI)React 组件的 TS 接口?
- javascript - 刷新后保持css不变
- compilation - 目标文件的符号表
- react-native - 反应本机底部选项卡未显示
- python - 从烧瓶应用程序中的 url 下载文件