Laravel 8 測試視圖

2021-07-19 11:56 更新

Laravel 允許在不向應用程序發(fā)出模擬 HTTP 請求的情況下獨立呈現(xiàn)視圖。為此,可以在測試中使用 view 方法。view 方法接受視圖名稱和一個可選的數(shù)據(jù)數(shù)組。這個方法返回一個 Illuminate\Testing\TestView 的實例,它提供了幾個方法來方便地斷言視圖的內(nèi)容:

public function testWelcomeView()
{
    $view = $this->view('welcome', ['name' => 'Taylor']);

    $view->assertSee('Taylor');
} 

TestView 對象提供了以下斷言方法:assertSee,assertSeeInOrder,assertSeeText,assertSeeTextInOrder,assertDontSeeassertDontSeeText

如果需要,你可以通過將 TestView 實例轉換為一個字符串獲得原始的視圖內(nèi)容:

$contents = (string) $this->view('welcome'); 
以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號