How to run a single seeder during unit test? |
I can't get it to work. I set basepath and seeder as per the documentation and get the following errors:
The seeders are in project/tests/_support/Database/Seeds/ tests/_support/Database/Seeds/TestSeeder.php: PHP Code: <?php With: PHP Code: protected $seed = 'TestSeeder'; I get: Code: 1) App\Controllers\UsersTest::testCreateUsuarioNuevo Error: Class 'App\Database\Seeds\TestSeeder' not found With: PHP Code: protected $seed = 'TestSeeder'; I get: Code: 1) App\Controllers\UsersTest::testCreateUsuarioNuevo InvalidArgumentException: The specified seeder is not a valid file: /tests/_support/Database/Seeds/TestSeeder.php I'm running PHP 7.4.33 on wamp64 and CodeIgniter 4.2.11.
Database seeds are simple classes that must have a run() method, and extend CodeIgniter\Database\Seeder. Within the run() the class can create any form of data that it needs to. It has access to the database connection and the forge through $this->db and $this->forge, respectively. Seed files must be stored within the app/Database/Seeds directory. The name of the file must match the name of the class.
What did you Try? What did you Get? What did you Expect?
Joined CodeIgniter Community 2009. ( Skype: insitfx )
(02-25-2023, 01:13 AM)InsiteFX Wrote: Database seeds are simple classes that must have a run() method, and extend CodeIgniter\Database\Seeder. Within the run() the class can create any form of data that it needs to. It has access to the database connection and the forge through $this->db and $this->forge, respectively. Seed files must be stored within the app/Database/Seeds directory. The name of the file must match the name of the class. I'm sorry I wasn't clear enough, but I'm trying to seed the db to run unit tests. When I move the TestSeeder class into app/Database/Seeds I get this error: Code: 1) App\Controllers\UsersTest::testCreateUsuarioNuevo But when I move it back into tests/_support/Database/Seeds: Code: 1) App\Controllers\UsersTest::testCreateUsuarioNuevo Thanks for your answer!
If you're placing your file into app/Database/Seeds then your namespace is incorrect when you attempt to run it.
Code: Currently: namespace Tests\Support\Database\Seeds; If you're trying to see if your Seeder works try using the spark CLI and do the following: Code: php spark make:seeder Test --suffix The seeder will do nothing, but it will create the appropriate namespaces and methods for you to modify according to your needs. Once you have it functioning as intended, then you can worry about its physical location in your system and modify the namespace accordingly.
I got the exact same problem,
always : "Error: Class "App\Database\Seeds\UserSeeder" not found" although i'm in tests directory, and although i specifiy the right path to $basePath Can anyone help ? ![]() (04-15-2024, 07:06 AM)alice Wrote: I got the exact same problem, Im replying here because I have exactly the same problem, followed the documentation. |
Welcome Guest, Not a member yet? Register Sign In |