2013-06-01 104 views
1

我正在創建一個Laravel 4應用程序,我碰到了一個小障礙。在爲我的控制器編寫測試時,我注意到一些奇怪的原因,它似乎無法驗證。我的(剝離)控制器代碼。Laravel控制器UNIT測試問題?

<?php use Controllers\Base\PublicController; 

class GuestController extends PublicController { 

    /** 
    * Display the report issue form. 
    * 
    * @return null 
    */ 
    public function getReportIssue() 
    { 
     $this->layout->title = Lang::get('app.report_issue'); 

     $this->layout->content = View::make('guest.report_issue'); 
    } 

    public function postReportIssue() 
    { 
     $rules = [ 
      'full_name' => 'required|min:2|max:100', 
      'email'  => 'required|email', 
      'issue'  => 'required|min:10|max:1000', 
     ]; 

     $validator = Validator::make(Input::all(), $rules); 

     if ($validator->fails()) 
     { 
      return Redirect::route('guest.report_issue') 
       ->withInput() 
       ->withErrors($validator->messages()); 
     } 

     return Redirect::route('guest.reported_issue') 
      ->with('msg', 'Okay'); 
    } 
} 

現在他測試香港專業教育學院創建了上述兩種方法都...

public function testHandleFailReportIssue() 
{ 
    Input::replace([ 
     'full_name' => '', 
     'email'  => '', 
     'issue'  => '', 
    ]); 

    $this->call('POST', 'report-issue'); 

    $this->assertRedirectedToRoute('guest.report_issue'); 

    $this->assertSessionHasErrors(['full_name', 'email', 'issue']); 
} 

public function testHandlePassReportIssue() 
{ 
    Input::replace([ 
     'full_name' => 'John Doe', 
     'email'  => '[email protected]', 
     'issue'  => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar. 
         Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar' 
    ]); 

    $this->call('POST', 'report-issue'); 

    $this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']); 
} 

初始測試順利通過,但第二個失敗。經過一些調查,它顯示驗證沒有通過,這意味着Input::replace()方法沒有完成它的工作,因爲我注入了有效的請求值。也許我錯過了一些事情嗎?

[編輯]

我決定做這個

public function testHandlePassReportIssue() 
{ 
    Input::replace([ 
     'full_name' => 'John Doe', 
     'email'  => '[email protected]', 
     'issue'  => 'Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar. 
         Lorem ipsum idom lola singel tudor reopmatica loesn dolor gotar', 
    ]); 

    $response = $this->route('POST', 'guest.report_issue'); 

    dd($this->app['session']->get('errors')); 

    $this->assertRedirectedToRoute('guest.reported_issue', [], ['msg']); 
} 

在測試通過檢查會話調試和像我懷疑,輸入沒有得到填充,任何原因,這可能發生?驗證消息已返回。

回答

6
$response = $this->route('POST', 'guest.report_issue', array(
    'full_name' => 'Foo', 
    'email' => '[email protected]', 
    'issue' => 'FooBar')); 

您可以將參數作爲數組傳遞。

+0

感謝問題解決! – CreativityKills