Error in laravel testing : SQLSTATE[HY000]: General error: 1 no such table: users

when I use PHPUnit in laravel and add a factory to my own test class after running PHPUnit appear this error in the console:

 SQLSTATE[HY000]: General error: 1 no such table: users

my test class method:

public function testExistSomeTextsInIndexPage()
{
    $users= factory(User::class)->create();
    $this->get('/')->assertSee($users->name);
     
}

this factory code work correctly in other parts of my project just show the error in the test class

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You should include the IlluminateFoundationTestingDatabaseMigrations as a trait. There is documentation about this.

use IlluminateFoundationTestingDatabaseMigrations;
use TestsTestCase;

class ExampleTest extends TestCase
{
    use DatabaseMigrations;
}

Note: Set the correct DB credentials. If you use your credentials for you existing DB, it will override the DB. I would advise you to use a in-memory DB with SQLite, see example here


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x