Search code examples
laravellaravel-testing

Laravet test case failing, unable to run seed on test case


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.


Solution

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