jgravois
1 month ago
469
8
Testing

Seeding For PEST Test

Posted 1 month ago by jgravois

I am trying to seed a database table from a JSON file for a specific set of tests.

This is the seeder saved in the regular seeds folder (generated by php artisan make:seeder)

use App\Models\Deal;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;

class DealsTestSeeder extends Seeder
{
    public function run()
    {
        DB::table('deals')->delete();

        $json = File::get("database/json/deals.json");
        $data = json_decode($json);

        foreach($data as $obj) {
            Deal::create($obj);
        } // end foreach
    }
}

This is the beginning of the test file

use App\Models\Deal;
use Illuminate\Support\Facades\Storage;

test('report returns deals of status 8', function() {
    $this->seed(DealsTestSeeder::class);

    $this->assertEquals(100, Deal::all()->count());
});

I GET THIS:

Tests\Feature\ProjectPerformanceReportTest > report returns deals of status 8
   Illuminate\Contracts\Container\BindingResolutionException

  Target class [DealsTestSeeder] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:807
    803▕
    804▕         try {
    805▕             $reflector = new ReflectionClass($concrete);
    806▕         } catch (ReflectionException $e) {
  ➜ 807▕             throw new BindingResolutionException("Target class [$concrete] does not exist.", 0, $e);
    808▕         }
    809▕
    810▕         // If the type is not instantiable, the developer is attempting to resolve
    811▕         // an abstract type such as an Interface or Abstract Class and there is

      +25 vendor frames
  26  tests/Feature/ProjectPerformanceReportTest.php:7
      Illuminate\Foundation\Testing\TestCase::seed("DealsTestSeeder")

  Tests:  1 failed
  Time:   0.57s

Please sign in or create an account to participate in this conversation.