jpmg's avatar
Level 13

Problem with Test

hello everyone. I starting to creating test for my application, and I have this problem that i don´t know how to fix it. in My AuthServiceProvider in my boot method I have this code.

public function boot(Gate $gate)
 {
    parent::registerPolicies($gate);
     foreach ($this->getAction() as $data) {
           $gate::define($data->name_action, function ($user) use ($data) {
               return $user->hasRole($data->roles);
           });
       }
}

private function getAction()
 {
    return Action::with('roles')->get();
}

So when i want to run my test i get this error

		Undefined table: 7 ERROR:  relation "actions" does not exist LINE 1: select * from actions" 
       ^ (SQL: select * from "actions")

I understand the problem, but how can I run my test if is looking for a table that hasn't been loaded yet?

0 likes
4 replies
LaryAI's avatar
Level 58

It seems like you're encountering a common issue where your test database hasn't been set up with the necessary tables. To resolve this, you need to make sure that your test database is properly migrated before running your tests. Here's what you can do:

  1. Ensure you have a separate database configuration for your testing environment. You can set this up in your phpunit.xml file or in your .env.testing file.

  2. Run migrations on your test database before executing your tests. You can do this by calling the artisan migrate command with the --env=testing option if you're using a separate .env.testing file, or by using the RefreshDatabase trait in your test classes.

Here's an example of how you can use the RefreshDatabase trait in your test:

use Illuminate\Foundation\Testing\RefreshDatabase;

class YourTest extends TestCase
{
    use RefreshDatabase;

    // Your test methods here
}

The RefreshDatabase trait will take care of migrating your test database before each test runs, ensuring that all the necessary tables are present.

If you prefer to run migrations manually, you can do so with an artisan command:

php artisan migrate --env=testing

Make sure that your test database connection is configured correctly in your config/database.php file or in your .env.testing file.

By following these steps, your test database should have the actions table, and you should no longer see the error about the missing table when running your tests.

Aleksa_Innsbruck's avatar

hello @jpmg , did you prepared everything and tested your database and tables? @gych i think that he did not create a table in DB. Either in DB Client or over Laravel Migration. you can use any DB Client like Mysql Workbench to test connection parameters from .env file in laravel and to check if "php artisan migrate" command is executed with success. After that you will have confirmation that you can start Tests. so:

  1. create migration for particular table - Action
  2. execute php artisan migrate
  3. you should have table Action in DB (check in Workbench) did you finished all the steps and checked your DB and tables exists? as @gych said, you can use the Factories to load data in tables, but if table is not created you can do anything.
1 like
Aleksa_Innsbruck's avatar

and btw @jpmg , put some checking in that code where you fetching data and then executes foreach or call a method. try/catch or if at least.

Please or to participate in this conversation.