0

我正在尝试编写带有一些断言的测试。这是一个简单的 GET 请求示例,我希望它会失败,因为页面上缺少文本。但是,它给了我绿色。当我检查响应内容时,我清楚地看到它抛出错误。我正在使用 laravel 8。

<?php

namespace Tests\Feature;

use Carbon\Carbon;
use Tests\TestCase;
use App\Models\Concert;
use Illuminate\Foundation\Testing\DatabaseMigrations;

class ViewConcertListingTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    function user_can_view_a_concert_listing() {
        $concert = Concert::create([
            'title' => 'The Red Chord',
            'subtitle' => 'with Animosity and Lethargy',
            'date' => Carbon::parse('December 13, 2016 8:00pm'),
            'ticket_price' => 3250,
            'venue' => 'The Mosh Pit',
            'venue_address' => '123 Example Lane',
            'city' => 'Laraville',
            'state' => 'ON',
            'zip' => '17916',
            'additional_information' => 'For ticket, cal (555) 555-5555.',
        ]);

        $view = $this->get('/concerts/'.$concert->id);

        $view->assertSee('The Red Chord');
        $view->assertSee('with Animosity and Lethargy');
        $view->assertSee('December 13, 2016');
        $view->assertSee('8:00pm');
        $view->assertSee('32.50');
        $view->assertSee('The Mosh Pit');
        $view->assertSee('123 Example Lane');
        $view->assertSee('Laraville, ON 17916');
        $view->assertSee('For tickets, call (555) 555-5555.');
    }
}


4

1 回答 1

-1
 protected function setUp() {
    parent::setUp(); 
    $this->withoutExceptionHandling();

}

laravel 会自动“处理”幕后的异常,并且不会在我们的测试中抛出它。为了自己处理这个问题,我们需要使用 withoutExceptionHandling() 方法

于 2020-10-10T14:38:47.693 回答