This happens to me as well. I think it has to do with the error page that Laravel displays by default when an error occurs.
Just add this at the top of your tests.
$this->withoutExceptionHandling();
Once the test pass for real you can remove it.
I have a simple test in which i am creating a model instance and then. i am visiting a route to check the details of that model object. i have just defined a controller file and there is no function defined in there. The view file is not created. then i am asserting to see some string in the blade file. l am sharing all codes. yet i see all test passing when it should fail.
test case
<?php
namespace Tests\Feature;
use App\Models\Concert;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Tests\TestCase;
use Throwable;
class ViewTicketConcertListing extends TestCase
{
use RefreshDatabase;
/** @test */
public function user_can_view_a_concert_listing()
{
// Arrange
$concert = Concert::create([
'title' => 'The Red Chord',
'subtitle' => 'with Animosity and Lethargy',
'date' => Carbon::parse('December 13, 2016 8:00pm'),
'ticket_price' => 3250, // better way to store currency ( in lowest possible rate)
'venue' => 'The Mosh Pit',
'venue_address' => '123 Lane Street',
'city' => 'Laraville',
'state' => 'ON',
'zip' => '17916',
'additional_information' => 'For tickets, call (555) 555-5555',
]);
// Act
// $this->withoutExceptionHandling();
$response = $this->get('/concerts/' . $concert->id);
// Assert
$response->assertSee('The Red Chord');
$response->assertSee('with Animosity and Lethargy');
$response->assertSee('December 13, 2016 8:00pm');
$response->assertSee('32.50');
$response->assertSee('The Mosh Pit');
$response->assertSee('123 Lane Street');
$response->assertSee('Laraville');
$response->assertSee('ON');
$response->assertSee('17916');
$response->assertSee('For tickets, call (555) 555-5555');
}
}
routes/web.php
Route::get('/concerts/{concert}', [ConcertsController::class, 'show']);
`app\Http\Controllers\ConcertsController.php'
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ConcertsController extends Controller
{
//
}
as it can be seen that nothing is defined here. yet i see all test passing.
> Executing task: /home/rex/rajesh/code/ticketbeast/vendor/bin/phpunit /home/rex/rajesh/code/ticketbeast/tests/Feature/ViewTicketConcertListing.php --filter '^.*::user_can_view_a_concert_listing( .*)?$' <
PHPUnit 9.5.2 by Sebastian Bergmann and contributors.
. 1 / 1 (100%)
Time: 00:00.348, Memory: 14.00 MB
OK (1 test, 10 assertions)
Terminal will be reused by tasks, press any key to close it.
but when i add $this->withoutExceptionHandling() in the test. then i see correct error. i know that the test should fail yet it is passing. is there something i am missing during testing ??
I am using laravel 8 with default packages.
UPDATE: now i made show function and returned a view instance. but the blade file is not created yet. still the test is passing.
That is the trade off, unfortunately.
I take it you are doing feature tests.
You could maybe do an assertion on something that does not exists in your view but does in the graceful exception handling from Laravel.
$this->assertNotSee();
I always turn off the exception handling until I get the test to pass and then turn it back on. There might be cases where it fails the test due to the turned off exception handling but you shouldn't use it in the finished test.
Please or to participate in this conversation.