2015-05-25 60 views
2

我在L5.1中使用了新的集成測試工具。運行測試類時出現一些奇怪的錯誤 - 我測試的頁面上大部分是404錯誤。奇怪的是,當我過濾到一個單獨的測試時,測試通過的很好。當我運行它所屬的整個測試類或測試套件時,它會以404失敗。該路由在瀏覽器中運行,並且測試在我自己運行時通過,因此它顯然不是有效的錯誤。Laravel集成測試作爲一個類運行時拋出虛假404錯誤,單個測試正常工作

的代碼看起來是這樣的:

class MyTest extends \TestCase 
{ 
use WithoutMiddleware; 

public function __construct() { 

    parent::__construct(); 

    $this->mock = m::mock('MyApp\Stuff\Repository'); 
    $this->validator = m::mock('Illuminate\Validation\Factory'); 
    $this->mockedClass = m::mock('MyApp\Stuff\Service'); 
} 

/** 
* @test 
*/ 
public function it_should_get_all_thingies() { 
    $this->mockedClass->shouldReceive('someMethod') 
     ->once() 
     ->andReturn('yay'); 
    $this->app->instance('MyApp\Stuff\Service', $this->mockedClass); 

    $this->visit('/api/v1/thingies'); 
    } 
} 

當我運行 phpunit --filter=it_should_get_all_thingies,它工作正常。 當我運行 phpunit --filter=MyTest,它與404一起死亡。我可以將錯誤消息中的相關URL複製到瀏覽器中,它工作正常。

我能想到的唯一的其他相關事實是,這個從L4.2升級到5.0到5.1。

任何幫助,非常感謝。

回答

7

找出觸發錯誤的原因以及如何解決該錯誤。我拉着這樣的二級路由文件:

Route::get('/', function() { 
return view('welcome'); 
}); 

Route::group(['prefix' => 'api/v1'], function() { 
require_once_app_path(__DIR__ . '/routes2.php'); 

}); 

require_once_app_path觸發此問題。沒有發生在L4.2或L5.0,當我們升級到5.1時開始。用require代替它似乎可以解決問題。

+2

你是我靈魂的救星,我欠你一輩子。我使用'require_once'將路由分解成單獨的文件,並將其切換爲'require'來修復所有內容。 – Leng

+0

這樣做有沒有其他的效果,如路線被添加兩次? – astroanu

相關問題