2014-02-24 90 views
4

我有在Laravel 4Laravel 4測試環節

測試控制器一個問題,我有下面的代碼:

public function getRemind() 
{ 
    $status = \Session::get('status'); 
    $error = \Session::get('error'); 
    $email = \Session::get('email'); 

    return \View::make('admin/reminds/remind_form', compact('status', 'error', 'email')); 
} 

我要測試是否在視圖中由控制器傳遞正確的數據:

public function testGetRemind() 
{ 
    \Session::set('status', 'status'); 
    \Session::set('error', 'error'); 
    \Session::set('email', 'email'); 
    $response = $this->action('GET', 'Admin\[email protected]'); 
    $this->assertTrue($response->isOk(), 'Get remind action is not ok'); 
    $this->assertViewHas('status', 'status'); 
    $this->assertViewHas('error', 'error'); 
    $this->assertViewHas('email', 'email'); 
} 

但是這不起作用。

另外我不能模擬會話類,因爲它不被框架允許 - 當我嘗試這樣做時會出現很多錯誤。

回答

10

在測試開始時致電Session::start()。在測試中調用URL時,如果會話尚未啓動,則會啓動該會話,擦除放入其中的任何現有數據。

既然v4.1.23,你也可以做$this->session($arrayOfSessionData),它爲你處理會話的開始。