netdjw
netdjw

Reputation: 6007

Laravel Dusk says Class 'Tests\Browser\Components\...' could not be found

After I installed laravel-dusk following the official Laravel documentation and run this command:

php artisan make:component CardComponentTest

Then try to run immediately:

php artisan dusk tests/Browser/Components/CardComponentTest.php

I get this error:

Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.

I tested file and path are correct:

ls -l /var/www/html/tests/Browser/Components/CardComponentTest.php

And it says:

-rw-r--r-- 1 djw djw 6917 Dec  3 11:25 /var/www/html/tests/Browser/Components/CardComponentTest.php

So it is exists and readable.

I checked the namespace in the file:

<?php

namespace Tests\Browser\Components;

It also looks good.

I checked the composer.json and in this I have this section:

    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

So the file exists, the namespace is good and the namespace is picked up in the composer.json.

I tried to run composer dump-autoload too. All good.

Any ide what's wrong whit this?

Upvotes: 1

Views: 405

Answers (2)

Muhammad Dyas Yaskur
Muhammad Dyas Yaskur

Reputation: 8098

the error:

Class 'Tests\Browser\Components\CardComponentTest' could not be found in '/var/www/html/tests/Browser/Components/CardComponentTest.php'.

indicate you don't have CardComponentTest test case. Because CardComponentTest is not test case but just an component/part of test.

As aless said, component is not a test itself. But if you really want to test CardComponentTest you can replace

use Laravel\Dusk\Component as BaseComponent;

class CardComponentTest extends BaseComponent

to :

use Tests\DuskTestCase;
 
class CardComponentTest extends DuskTestCase

after you do that you can run php artisan dusk tests/Browser/Components/CardComponentTest.php , You just need to add test method first like. An example:

public function testComponentExample()
{
    $this->browse(function (Browser $browser) {
        $browser->visit('/')
                ->assertSee('Laravel');
    });
}

But create test case in component folder is not best practice. You should create test case like official documentation said. Run dusk:make instead of dusk:component. An example:

php artisan dusk:make SimpleBrowserTest

and now you can run

php artisan dusk tests/Browser/SimpleBrowserTest.php

Upvotes: 0

Aless55
Aless55

Reputation: 2709

If you take a look at the documentation, the Component is not a test itself. Hence, you should not name the component "*Test" and you should not try to run the component like a dusk test.

You will use your component in your dusk test as described here: https://laravel.com/docs/9.x/dusk#using-components

Components are for repeated actions, like selecting a date in a picker, which is then further used in your test case for example in a form or maybe some change in your UX happens that is assertable.

Upvotes: 0

Related Questions