0

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
  • 39
  • 7
  • 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

2 Answers2

5

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
  • 2,033
  • 1
  • 11
  • 25
1

it worked for me when i insert use RefreshDatabase;

Like this:

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

class ExampleTest extends TestCase
{
    use RefreshDatabase;

}

Note: Just be careful in my case I use Laravel :memory database, this function deletes all the data that was created