-1

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

kaveh.hd
  • 29
  • 5
  • 1
    did you migrate in the setup of the test ? – N69S Jul 09 '21 at 08:02
  • where is the setup of the test exactly? I just migrate the main database project – kaveh.hd Jul 09 '21 at 08:06
  • 1
    At the root folder, you have the config file for phpunit, set it to use the same database as the project. usualy, for tests you use a temporary database so you can test everything (creation, modification and deletion). – N69S Jul 09 '21 at 08:10

1 Answers1

2

You should include the Illuminate\Foundation\Testing\DatabaseMigrations as a trait. There is documentation about this.

use Illuminate\Foundation\Testing\DatabaseMigrations;
use Tests\TestCase;

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

Eric Landheer
  • 1,719
  • 10
  • 22