doutanggun9816 2016-03-31 07:36
浏览 81

Symfony功能测试失败,但同样的请求在浏览器中有效

I followed the Symfony documentation about functional tests in order to write my first one, but I have some issues. The response I get via browser works good:

Browser response

But when I run phpunit -c app/ in the shell I get a failure.

1) AppBundle\Tests\Controller\MeterAPIControllerTest::testGetAllVariables Failed asserting that 500 matches expected 200.

This is the code:

<?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()); 
    }
}

If I try another test assertion, I get failure too.

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

EDIT

When I run phpunit in app/logs/test.log I get a PHP Exception:

[2016-03-31 15:25:21] request.CRITICAL: Uncaught PHP Exception Doctrine\Common\Persistence\Mapping\MappingException: "Invalid mapping file 'AppBundle.Entity.EM2Meter.orm.yml' for class '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): Invalid mapping file 'AppBundle.Entity.EM2Meter.orm.yml' for class 'AppBundle\Entity\EM2Meter'. at /Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86)"} []

How can I fix this?

  • 写回答

2条回答 默认 最新

  • douyan9417 2016-03-31 10:02
    关注

    You have not told the symfony client to contact the localhost server on port 8000, its still defaulting to 80.

    When you instantiate your client, specify the host like this.

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

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等