jgravois's avatar

Seeding For PEST Test

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
0 likes
10 replies
Sergiu17's avatar
Sergiu17
Best Answer
Level 60

Looks like you have to import DealsTestSeeder class

use App\Models\Deal;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Seeder\DealsTestSeeder; // <=== this line

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

@sergiu17 That worked for me for one seeder.

<?php

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

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

    $this->assertEquals(34, Deal::active()->count());
});

This gives me

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

  Target class [DealsFinSeeder] 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:8
      Illuminate\Foundation\Testing\TestCase::seed("DealsFinSeeder")

To be more logical I created a TestingDatabaseSeeder which looks like this

use Illuminate\Database\Eloquent\Model;

class TestingDatabaseSeeder extends Seeder
{
    public function run()
    {
        Model::unguard();
        $this->call(DealsTestSeeder::class);
        $this->call(DealsFinSeeder::class);
        Model::reguard();
    }
}

However

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

$this->assertEquals(34, Deal::active()->count());

});


results in our old friend

Target class [DealsFinSeeder] 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

  +8 vendor frames

9 database/seeds/TestingDatabaseSeeder.php:12 Illuminate\Database\Seeder::call("DealsFinSeeder")

jgravois's avatar

STUPID ME!

composer dump-autoload

Thanks again!

TobiasS's avatar

I get the same error, unfortunately the composer dump-autoload does not solv it


use Illuminate\Database\Seeder\DepartmentSeeder;

it('creates a new user', function () {
    $this->seed(DepartmentSeeder::class);

Error

• Tests\Feature\ExampleTest > it creates a new user
   Illuminate\Contracts\Container\BindingResolutionException 

  Target class [Illuminate\Database\Seeder\DepartmentSeeder] does not exist.

  at vendor/laravel/framework/src/Illuminate/Container/Container.php:811

Any idea what's wrong?

TobiasS's avatar

@sergiu17 Yes I have the Seeders classes inside database/seeds/ folder.

@jgravois I have created the seeders

It works perfect using the "normal" laravel/phpUnit test

<?php
namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use DepartmentSeeder;

/** @test */
public function test_seed()
      {
          $this->seed(DepartmentSeeder::class);

But when I try with PEST Target class [Illuminate\Database\Seeder\DepartmentSeeder] does not exist.

I have tried with both the normal laravel seeder

<?php

use Illuminate\Database\Seeder;

class DepartmentSeeder extends Seeder

and what I use for some seeders (seeding Csv) https://github.com/jeroenzwart/laravel-csv-seeder

But as soon as I try to implement it in PEST it fails

da_Mask's avatar

@TobiasS Use the fully qualified name when referencing your seeder in your tests:

<?php
namespace Tests\Feature;

use Database\Seeders\DepartmentSeeder;

And make sure your namespace is set in your seeder file:

<?php
namespace Database\Seeders;

Please or to participate in this conversation.