【发布时间】:2021-01-25 09:05:32
【问题描述】:
我正在尝试编写带有一些断言的测试。 这是一个简单的 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.');
}
}
【问题讨论】:
-
“当我检查响应内容时,我清楚地看到它抛出错误”这是什么意思?你看到什么错误信息?一些建议:您应该研究创建测试模型的工厂,应该使用
route()帮助程序而不是 URL,并且可以通过链接方法来减少代码。例如。$this->get('/concerts/' . $concert->id)->assertSee('x')->assertSee('y')->assertDontSee('z'); -
测试没有失败,但是成功了。这就是我所说的“当我检查响应内容时,我清楚地看到它抛出错误”。
标签: php laravel phpunit laravel-8