Symfony功能测试失败,但相同的请求在浏览器中有效 [英] Symfony functional test fail but the same request works in browser

查看:110
本文介绍了Symfony功能测试失败,但相同的请求在浏览器中有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遵循了

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

1)AppBundle \ Tests \ Controller \ MeterAPIControllerTest :: testGetAllVariables无法断言500个匹配预期的200个.

这是代码:

 <代码><?php命名空间AppBundle \ Tests \ Controller;使用Symfony \ Bundle \ FrameworkBundle \ Test \ WebTestCase;MeterAPIControllerTest类扩展了WebTestCase{公共功能testGetAllVariables(){$ client = static :: createClient();$ crawler = $ client-> request('得到','/meters/121/120/variables');//声明特定的200状态代码$ this-> assertEquals(200,$ client-> getResponse()-> getStatusCode());}} 

如果我尝试另一个测试断言,我也会失败.

 //断言"Content-Type"标头是"application/json"$ this-> assertTrue($ client-> getResponse()->标头->包含('内容类型','application/json')); 

编辑

当我在 app/logs/test.log 中运行 phpunit 时,我得到一个PHP异常:

[2016-03-31 15:25:21]请求.严重:未捕获的PHP异常Doctrine \ Common \ Persistence \ Mapping \ MappingException:无效的映射类的文件"AppBundle.Entity.EM2Meter.orm.yml""AppBundle \ Entity \ EM2Meter"./Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php第86行{"exception":"[object](Doctrine \ Common \ Persistence \ Mapping \ MappingException(代码:0):类别的无效映射文件'AppBundle.Entity.EM2Meter.orm.yml'"AppBundle \ Entity \ EM2Meter".在/Applications/MAMP/htdocs/iso50k1_tst_symfony/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:86)}[]

我该如何解决?

解决方案

您尚未告诉symfony客户端联系端口8000上的localhost服务器,该端口仍默认为80.

实例化客户端时,请指定主机.

  $ client = static :: createClient([],['HTTP_HOST'=>'本地主机:8000',]); 

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:

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?

解决方案

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',
] );

这篇关于Symfony功能测试失败,但相同的请求在浏览器中有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆