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

3
  • 1
    did you migrate in the setup of the test ? Commented Jul 9, 2021 at 8:02
  • where is the setup of the test exactly? I just migrate the main database project Commented Jul 9, 2021 at 8: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). Commented Jul 9, 2021 at 8:10

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.