gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:43:51 27 4
gpt4 key购买 nike

我关注了 Symfony documentation关于功能测试以便编写我的第一个,但我有一些问题。我通过浏览器获得的响应效果很好:

Browser response

但是当我在 shell 中运行 phpunit -c app/ 时,我失败了。

1) AppBundle\Tests\Controller\MeterAPIControllerTest::testGetAllVariables Failed asserting that 500 matches expected 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-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)"} []

我该如何解决这个问题?

最佳答案

你没有告诉 symfony 客户端在端口 8000 上联系本地主机服务器,它仍然默认为 80。

当您实例化您的客户端时,像这样指定主机。

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

关于php - Symfony 功能测试失败,但相同的请求在浏览器中有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36326998/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com