Testing attaching a file with Dusk - works via Tinker but not from the Test

Viewed 3328

I have a file uploader for which I have written the following test:

$browser->visit('/product/professional-photo-prints')
        ->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

That doesn't upload the file. No error either.

However, when I do this:

$browser->visit('/product/professional-photo-prints')
        ->tinker();

and once in the Terminal launched by Tinker, when I enter the exact same file uploading command I am using in the test:

$browser->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

...it works. I can see my Chrome instance upload the file and all.

I can't figure out for the life of me what could be the difference between that command being run from the test itself, or from Tinker.

Any help would be greatly appreciated. Thank you.

P.S. I am using Laravel 5.5 and Dusk 2.0

2 Answers

DIR magic constant refers to the parent folder of the file being executed.

->attach('photo', __DIR__.'/storage/app/public/testing/test_upload.jpg');

Of course there is no file found by this path. You should try this instead:

->attach('photo', storage_path('app/public/testing/test_upload.jpg'));

In addition, if your test file is inside your public (for example), you can use base_path;

e.g: base_path("public/sample-recording/test.mp3")

Related