2012-07-17 109 views
2

即使請求的頁面不存在,我總是會在我的測試用例中得到狀態代碼200/ok。phpunit中錯誤的狀態代碼

public function testCompleteScenario() 
{ 
    $client = static::createClient(); 

    $container = $client->getContainer(); 
    $kernel = $client->getKernel(); 

    $crawler = $client->request('GET', '/somethingnotexisting'); 
    $this->assertTrue(200 === $client->getResponse()->getStatusCode()); 

} 

我使用phpunits與NetBeans 7.1和我的symfony的防火牆配置爲只允許認證的用戶。否則,他們會重定向到登錄頁面。

有可能是一些php.ini調優?

編輯:

這裏是代碼,我從NetBeans中:

enter image description here

+2

當您使用瀏覽器瀏覽網頁時,您會獲得什麼狀態代碼?它是否將您重定向到登錄頁面,還是您獲得了404?也許這就是問題所在! – Sgoettschkes 2012-07-17 18:24:17

+1

另外考慮使用'assertEquals'而不是'assertTrue':'$ this-> assertEquals(200,$ client-> getResponse() - > getStatusCode());'。當這個測試失敗時,你會知道你有哪些狀態碼,而不是200. – AdrienBrault 2012-07-17 19:54:29

+0

當我瀏覽一個不存在的頁面時,我有一個錯誤404. – Chopchop 2012-07-18 10:13:14

回答

0

我花了3天嘗試解決這個問題。

我已將我的PHPUnit版本從3.6.11降級到3.5.14。

它現在對NetBeans更好用

4

首先做@AdrienBrault建議是什麼,使用的assertEquals:

$this->assertEquals(200, $client->getResponse()->getStatusCode()); 

這種方式,你會查看返回的狀態碼。

因爲功能測試客戶端默認不會重定向,所以您可能會獲得301或302(重定向)。

嘗試這樣:

public function testCompleteScenario() 
{ 
    $client = static::createClient(); 

    $container = $client->getContainer(); 
    $kernel = $client->getKernel(); 

    $client->request('GET', '/somethingnotexisting'); 
    $crawler = $client->followRedirect(); 

    $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
} 

參見:http://symfony.com/doc/current/book/testing.html#redirecting

+0

這個問題沒有進入狀態碼的測試,而是處於狀態代碼本身。我添加了一個打印屏幕,向您展示我使用更奇怪的URL獲得的狀態碼 – Chopchop 2012-07-18 11:01:25

相關問題