LoginSignup
0
0

More than 1 year has passed since last update.

【PHPUnitエラー】Maximum retries of 10000 reached without finding a unique value

Posted at

環境

Laravel v9.5.1 (PHP v8.1.3)

状況

factoryをいくつか作成すると下記のエラーが出た。
このエラー文でググっても原因が分からず、解決に時間がかかったので備忘録とする。

Maximum retries of 10000 reached without finding a unique value
class CampanyFactory extends Factory
{
  public function definition(): array
  {
    return [
      'name' => $this->faker->company(),
      'domain' => $this->faker->unique()->safeEmailDomain(),
    ];
  }
}

原因

safeEmailDomain()example.com, example.net, example.orgをランダムで出してくれるfakerだが、
今回domainカラムにユニーク制約をしていたため、domainが重複していたことで一見関係ないエラーが出てしまった。

解決法

今回はfakerを使用せず、下記のようにすることでドメインを再現した。
もっといい方法があったらぜひ教えていただきたいです。

class CampanyFactory extends Factory
{
  public function definition(): array
  {
    return [
      'name' => $this->faker->company(),
      'domain' => Str::random(10).'.com',
    ];
  }
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0