LoginSignup
0
0

More than 1 year has passed since last update.

【PHPUnit】ポイントの計算などのテストは固定値よりFakerを使う方がよい

Posted at

環境

Laravel v9.5.1 (PHP v8.1.3)

Tips:ポイントの計算のテストはFakerを使う

ポイントを合計するメソッドのテスト

$amount1 $amount2にそれぞれ数字のレンジに幅を持たせて、どんな数字の組み合わせのケースでも動くことを検証できるようにする。
ランダム性を持たせた方が網羅性が増すようなものはFakerを使う方がいい。

public function testSumPoints()
{
    $user = User::factory()->create();
    $amount1 = Faker\Factory::create()->randomFloat(1, 10, 100);
    $amount2 = Faker\Factory::create()->randomFloat(1, 10, 100);

    $results = PointHistory::sumPoints($user->id);
    $this->assertEquals($results, $amount1 + $amount2);
}

固定値で確認したいものに関してはベタ書きがいい。

public function testSumPoints()
{
    $user = User::factory()->create();
    $amount1 = 10;
    $amount2 = 50;

    $results = PointHistory::sumPoints($user->id);
    $this->assertEquals($results, $amount1 + $amount2);
}
0
0
1

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