Laravel Response::download() test Laravel Response::download() test laravel laravel

Laravel Response::download() test


$response->assertDownload() was added in Laravel 8.45.0:

Assert that the response is a "download". Typically, this means the invoked route that returned the response returned a Response::download response, BinaryFileResponse, or Storage::download response:

$response->assertDownload();

Learn More:

https://laravel.com/docs/8.x/http-tests#assert-download


EDIT: As pointed by @DavidBarker in his comment to the OP question

The Illuminate\Support\Facades\Response class doesn't actually extend Illuminate\Support\Facades\Facade so doesnt have the shouldRecieve() method. You need to test the response of this route after calling it in a test.


So if you want to test your download functionality, you can try checking the response for errors with:

$this->assertTrue(preg_match('/(error|notice)/i', $response) === false);


You can assert that the status code is 200

$this->assertEquals($response->getStatusCode(), 200);

because sometimes you might have some data returned that match "error" or "notice" and that would be misleading.

I additionally assert that there's an attachment in the response headers:

$this->assertContains('attachment', (string)$response);