2015-08-27 35 views
2

我寫這樣如何測試重定向fuelphp

public function action_submit() 
{ 
$submit = Format::forge(json_decode($_POST["submit"]))->to_array(); 
Servicecode::add_code_request($submit); 
Response::redirect('code/codedetail'); 
} 

然後我想寫PHPUnit來測試它的控制器,

public function test_adminsubmit() 
{ 
$Submit = array(...); 
$_POST["Submit"] = json_encode(Submit); 
$response = Request::forge('code/codeeditrequest/submit') 
    ->set_method('POST') 
    ->execute() 
    ->response(); 
$this->assertContains('ode Detail', $response->body->__toString()); 

什麼錯,它必須插入數據db,但是當它運行重定向時,我無法重定向頁面,所以測試失敗!WHy?這是怎麼回事?

回答

2

在我的理解中,你不能寫這樣的測試。

因爲Response::redirect()不返回任何內容,但只返回重定向的HTTP標頭,並且調用exit()。所以你的phpunit測試會被exit()中止。

要用Response::redirect()測試代碼,您必須以某種方式用測試double替換Response::redirect()方法。

+0

謝謝,你解決了我的麻煩! – yang