首页 > 解决方案 > 如何测试自动生成的 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());

标签: laraveltestingphpunit

解决方案


推荐阅读