2016-03-31 56 views
6

我跟着Symfony documentation關於功能測試,以寫我的第一個,但我有一些問題。我通過瀏覽器得到響應做工不錯:Symfony功能測試失敗,但相同的請求在瀏覽器中工作

Browser response

但是,當我在shell中運行phpunit -c app/我得到一個失敗。

1) 的appbundle \測試\控制器\ MeterAPIControllerTest :: testGetAllVariables 無法斷言,500場比賽預計200

這是代碼:

<?php 

namespace AppBundle\Tests\Controller; 

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; 

class MeterAPIControllerTest extends WebTestCase 
{ 
    public function testGetAllVariables() 
    { 
     $client = static::createClient(); 
     $crawler = $client->request(
      'GET', 
      '/meters/121/120/variables' 
     ); 

     // Assert a specific 200 status code 
     $this->assertEquals(200, $client->getResponse()->getStatusCode()); 
    } 
} 

如果我嘗試另一個測試斷言,我也失敗了。

// Assert that the "Content-Type" header is "application/json" 
$this->assertTrue(
    $client->getResponse()->headers->contains(
     'Content-Type', 
     'application/json' 
    ) 
); 

編輯

當我在app/logs/test.log運行phpunit我得到一個PHP異常:

[2016年3月31日15點25分21秒] request.CRITICAL:未捕獲的PHP例外 Doctrine \ Common \ Persistence \ Mapping \ MappingException:「無效映射 文件'AppBundle.Entity.EM2Meter.orm.yml'對於類 'AppBundle \ Entity \ EM2Meter'。」 at /Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php line 86 {「exception」:「[object] (Doctrine \ Common \ Persistence \ Mapping \ MappingException(code:0): 'AppBundle.Entity.EM2Meter.orm.yml'類映射文件'012' common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86)「} []

我該如何解決這個問題?

+2

你確定你的請求到正確的端口? – DevDonkey

+0

@DevDonkey是的,請求是正確的。 – Francesco

+1

你確定嗎?我想你會發現,除非你指定當你實例化一個新的客戶端,它會默認爲端口80. – DevDonkey

回答

1

你還沒告訴symfony的客戶聯繫本地主機服務器上的8000端口,它仍然默認爲80

當你實例化你的客戶,指定這樣的主機。

$client = static::createClient([], [ 
    'HTTP_HOST' => 'localhost:8000', 
]); 
+0

謝謝,但不幸的是這並沒有解決問題。 – Francesco

+0

服務器日誌說什麼? – DevDonkey

+0

我已更新該問題。 – Francesco

0

看起來這裏有緩存問題。我認爲這是一個很好的做法,清理緩存當前環境中運行測試之前和運行項目的功能測試前:

$ php bin/console cache:clear --env=dev

$ php bin/console cache:clear --env=tests

相關問題