2014-01-21 30 views
2

我正在對我的應用程序進行單元測試,當試圖使用TestCase::callSecure()route()助手調用控制器操作時,我得到一個NotFoundHttpExceptionroute()不能與Route :: enableFilters()配合使用

對於那些測試(只有幾個)我也啓用了過濾器Route::enableFilters

我filters.php:

App::before(
    function ($request) { 
     // force ssl 
     if (!$request->isSecure()) 
     { 
      return Redirect::secure($request->getRequestUri()); 
     } 
    } 
); 

// some authentication 
Route::filter('auth.api', 'Authentication'); 

我routes.php文件:

Route::post('login', array('as' => 'login', 'uses' => '[email protected]')); 
Route::post('register', array('as' => 'register', 'uses' => '[email protected]')); 

實例測試在那裏我得到異常:

$credentials = [ 
    // ... 
]; 

$response = $this->callSecure('POST', route('login'), $credentials); 

當我打電話的這些行動的路徑,它工作正常。

$credentials = [ 
    // ... 
]; 

$response = $this->callSecure('POST', '/login', $credentials); 

這是打算還是一個錯誤?

回答

2

route()幫助程序將生成給定命名路由的URL(包括相關協議,即http/s)。在你的情況下,它會返回類似:

https://example.com/login

這是不是你想要的。當您要執行重定向,例如這是有用的:

Redirect::route('login'); 

所以,你在你的最後一個例子做什麼,是正確的方法是做你想要的東西;因爲您不想將完整的URL作爲參數傳遞給您的callSecure()函數。

$response = $this->callSecure('POST', '/login', $credentials); 

而戴維已經提到的,你可以生成使用URL ::路線相對URL和$absolute參數缺省傳遞給true。例如,使用命名路由時,你可以使用以下命令:

$route = URL::route('profile', array(), false); 

將產生像/profile

0

route()幫手相對URL不會產生相對URL,這是你真正想要的。

要生成相對URL,您可以使用URL::route,因爲它允許您傳遞$absolute參數,該參數默認爲true。因此,使用一個名爲路線,讓您的相對URL,你可以做

$credentials = [ 
// ... 
]; 
$route = URL::route('login', array(), false); 
$response = $this->callSecure('POST', $route, $credentials); 

雖然'/login'方法是正確的,它違背了使用命名路由的目的,如果你還是要追捕,你必須將所有的地方如果/當你決定改變它,URL。