0

I'm getting error as on below screenshot

enter image description here

Please help me i am trying and for this i have spend too much time but still issue no luck.

I am using laravel official dusk package for front-web testing. When i am running login test case its showing error "users_email_unique" which is above showing in picture. I am using use DatabaseTransactions but it is not reverting my last transaction. For this i am using test database also. Here is my my code:

<?php

namespace Tests\Browser;

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

class LoginTest extends DuskTestCase
{
    use DatabaseTransactions;
    /**
     * A Dusk test example.
     *
     * @return void
     */

    public function test_I_can_login_successfully()
    {
        $user = factory(User::class)->create([
            'email' => 'login@gmail.com',
            'password' => bcrypt('password'),
        ]);

        $this->browse(function ($browser) use ($user) {
            $browser->visit('/')
                ->type('email', 'login@gmail.com')
                ->type('password', 'password')
                ->press('Login')
                ->assertSee($user->name);
        });
    }
}
Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
Ijaz Ali
  • 128
  • 9

1 Answers1

0

For Dusk you cannot use use DatabaseTransactions. You need to use DatabaseMigrations instead. When you update trait then you should get rid of the error.

You have sample Dusk test in documentation and it looks like this:

<?php

namespace Tests\Browser;

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

class ExampleTest extends DuskTestCase
{
    use DatabaseMigrations;

    /**
     * A basic browser test example.
     *
     * @return void
     */
    public function testBasicExample()
    {
        $user = factory(User::class)->create([
            'email' => 'taylor@laravel.com',
        ]);

        $this->browse(function ($browser) use ($user) {
            $browser->visit('/login')
                    ->type('email', $user->email)
                    ->type('password', 'password')
                    ->press('Login')
                    ->assertPathIs('/home');
        });
    }
}

As you see it's also using DatabaseMigrations trait.

Marcin Nabiałek
  • 109,655
  • 42
  • 258
  • 291
  • When i am using DatabaseMigrations trait. it deleted all the table from the database. And i am not using migration for creating tables. – Ijaz Ali Jan 04 '20 at 09:11
  • @IjazAli So that will be problem. You can try to manually empty tables at the end of each test. – Marcin Nabiałek Jan 04 '20 at 09:13
  • #Marcin Nabiałek you mean write separate function for deleting the entry. – Ijaz Ali Jan 04 '20 at 09:15
  • #Marcin Nabiałek please can you give me example or suggest some other way for solve this problem – Ijaz Ali Jan 04 '20 at 09:16
  • @IjazAli There is no other way. You can write method that you will run at the end of each test that will clear tables you want. For example DB::table('users')->truncate(); – Marcin Nabiałek Jan 04 '20 at 09:21
  • #Marcin Nabiałek i have used this but no luck. I have use include DB facade on top. – Ijaz Ali Jan 04 '20 at 09:26
  • @IjazAli It will help but first you need to have cleared table from the data. So on 1st run you can run it also at the beginning of test to clear data from table – Marcin Nabiałek Jan 04 '20 at 09:28
  • #Marcin Nabiałek now showing this error. Actual path [/login] does not equal expected path [/home]. Failed asserting that '/login' matches PCRE pattern "/^\/home$/u". – Ijaz Ali Jan 04 '20 at 09:35
  • @IjazAli So this is completely different problem. If you have new question ask new one – Marcin Nabiałek Jan 04 '20 at 09:36
  • #Marcin Nabialek thanks but my first problem solve. I am asking new question from a new comment. Thanks – Ijaz Ali Jan 04 '20 at 09:42
  • #Marcin Nabialek I am new so cannot mark up to your question. How can I want to appreciate your to your effort. – Ijaz Ali Jan 04 '20 at 09:49