gilmojoa's avatar

using a test database with dusk

hi,

I am trying to get my laravel dusk tests working with a test database and I have followed the pretty straight forward steps to do this with the database configuration and the .env.dusk.local file pointing to that test database configuration. its not working for me and there are a few questions I have to help me to understand why So firstly, the issue I have is that its not able to create a user for my application with factory method - im getting a - Error: Class 'Faker\Factory' not found.

I am using the databasemigrations trait in my test class. So when i use this trait, I assume this means that when i run my test, the migrations are run, my database is set up, the test is run and then the database migrations are rolled back? If this is correct assumption, then I should expect to see an empty test database after running my 'failing' test with above error?

I cant find any useful explanation as to why im getting above error, it seems to imply that my database migrations have not been executed and its not able to create a user on the user table?

0 likes
2 replies
SapporoGuy's avatar

You haven't provided code so it's hard to help.

  1. looks like faker is missing or not being called properly. You need to track that down.

For me:

I called my dusk env file: .env.dusk.testing (I'm still new at this but it works). I also use a test db and don't call migrations and such because dusk takes a long time to run lots of tests. I just refresh my test db every so often. Plus I like seeing the data for when I work straight in the browser.

gilmojoa's avatar

thanks for your reply. I will include the code below as reference. what i really want to understand is - should my test be using Faker/Factory when i am using the DatabaseMigrations trait? And is this error (Error: Class 'Faker\Factory' not found) relevant. I am on version laravel v5.5.

snippet of my test code:

use Tests\DuskTestCase; use Laravel\Dusk\Browser; use Illuminate\Foundation\Testing\DatabaseMigrations; use App\Models\User;

class CreateQuestionTest extends DuskTestCase {

public function testCreateUser() { //dd(env('APP_ENV'));

    $user = factory(User::class)->create([
        'name' => 'test user',
        'password'  => 'password',
        'email' => '[email protected]'
     ]);

.... }

snipped of env.dusk.local file (rest of properties repeated from .env file:

APP_ENV=local APP_KEY= APP_URL=http://localhost DB_CONNECTION=mysql_test DB_DATABASE=testdb APP_DEBUG=TRUE

and snippet of the database.php config:

'mysql_test' => [ 'driver' => 'mysql', 'host' => env('DB_HOST', '127.0.0.1'), 'port' => env('DB_PORT', '3306'), 'username' => env('DB_USERNAME', 'forge'), 'password' => env('DB_PASSWORD', ''), 'unix_socket' => env('DB_SOCKET', ''), 'database' => env('DB_DATABASE', database_path('mytestdb.mysql')), 'prefix' => '',

I created the empty mytestdb.mysql in the database dir.

This is exactly how I have seen it implemented from other posts here so i dont think i am missing anything. im trying to understand the use of this Faker/Factory class. Should my test really be using this class to create the user objects? if so, it looks like i am missing this class. did anyone else have to import this library for dusk tests??

Please or to participate in this conversation.