How to run a single seeder during unit test? - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: How to run a single seeder during unit test? (/showthread.php?tid=86904) |
How to run a single seeder during unit test? - Flurzo - 02-24-2023 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. RE: How to run a single seeder during unit test? - InsiteFX - 02-25-2023 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. RE: How to run a single seeder during unit test? - Flurzo - 02-25-2023 (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! RE: How to run a single seeder during unit test? - grimpirate - 02-25-2023 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. RE: How to run a single seeder during unit test? - alice - 04-15-2024 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 ? RE: How to run a single seeder during unit test? - kenjis - 04-15-2024 See https://codeigniter4.github.io/CodeIgniter4/testing/database.html#migrations-and-seeds |