laravel - 如何测试自动生成的 slug 是否独一无二?
问题描述
我正在使用的系统slug
从一个title
字段生成一个。title
不一定是唯一的,但确实slug
如此。
如果slug
确定 不是唯一的,系统会生成一个令牌以附加到 的末尾slug
以使其唯一。
由于user
不负责定义slug
,因此推荐的测试此工作流程的方法是什么?
slug
简单地测试数据库中不存在多次是否就足够了?例如:
$title = 'Ships in the Night';
$a = Model::factory()->create(['title' => $title]);
$b = $this->post('/endpoint', ['title' => $title]);
$this->assertCount(1, Model::where('slug', $a->slug)->get());
$this->assertCount(1, Model::where('slug', $b->slug)->get());
解决方案
推荐阅读
- sed - 这个 sed 命令:“sed -e :a -e '$d;N;2,10ba' -e 'P;D'” 是如何工作的?
- typo3 - 在 TYPO3 9.5.5 中为表单提交操作配置静态路由
- database - 每当更新数据库时,如何刷新表单?
- angular6 - 如何从 WebApi 在 Angular6 调度程序中的 Syncfusion 中填充数据?
- javascript - `new mongoose.Schema` 每次都使用相同的默认值
- string - 按位置将字符串拆分为元胞数组
- java - 使用java配置将sql/hql注入spring bean
- javascript - 如何修复 Slimmenu 多级子项目走出屏幕
- r - 如何在 2 个级别上聚合,首先是计数,其次是函数 f(count)?
- angular - 在javascript中将json对象转换为日期