0

My test case:

<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Illuminate\Http\Response;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class UserTest extends TestCase
{
    use RefreshDatabase, WithFaker, DatabaseMigrations;

    protected $endPoint = '/dashboard/users';

    public function setUp():void
    {
        parent::setUp();
        $this->artisan('migrate:fresh');
        $this->artisan('db:seed');
    }

    public function test_users_list_is_showing_correctly()
    {
        $this->signIn();
        $this->get($this->endPoint)
            ->assertStatus(Response::HTTP_OK);
    }

}

But, I am receiving error:

  SQLSTATE[HY000]: General error: 1 no such table: settings (SQL: select "id", "name", "setting_value" from "settings")

It might be throwing error because, I have this code in boot method of AppServiceProvider

config(['settings' => Setting::get(['id','name','setting_value'])]);

How to fix this? Its probably that migration and seeder are not working, but not sure.

Aayush Dahal
  • 856
  • 1
  • 17
  • 51
  • `RefreshDatabase` and `DatabaseMigrations` will both run the database migrations and you are also running them in your setup. Seems a bit of an overkill. It might also result in this error since running an artisan command will also run the service providers. I suggest using only `RefreshDatabase` and then seeding – apokryfos Sep 02 '21 at 05:26

1 Answers1

1

Assuming that you are using laravel 8.x and as it can be seen in code that you have used RefreshDatabase trait. You can do something like this:

 // Run the DatabaseSeeder...
    $this->seed();

 // Run a specific seeder...
  $this->seed(OrderStatusSeeder::class);

The above code is taken from the official documentation.

Alternatively, you can set

protected $seed = true;

in your base test case class. This will run the seeder before each test that uses RefreshDatabase trait.

You can also define which seeder should run by specifying this

protected $seeder = OrderStatusSeeder::class;

in your test class.

Hope this helps. More information you find here.

  • I have tried `$this->seed` as well but it is giving the same error, probably because, we have yet to run migration right? Or the migration will be run by default? – Aayush Dahal Sep 02 '21 at 05:17
  • Ideally, you should create a separate database for testing. This can be done by creating .env.testing file and providing the test database connection details. Then you can run migration with this command: php artisan migrate --env´=testing Then run the tests. – Paresh Maheshwari Sep 02 '21 at 06:42
  • thank you, I fix this issue but I am facing this new issue https://stackoverflow.com/questions/69028539/sqlstatehy000-general-error-1-on-conflict-clause-does-not-match-any-primary could you please help on this one? – Aayush Dahal Sep 02 '21 at 10:30