Symfony 5中的PHPUnit测试-单元测试始终返回404或500

时间:2020-06-23 14:46:16

标签: php unit-testing symfony phpunit symfony5

配置:

操作系统:MacOS Catalina 10.15.4
Web服务器:Apache(使用MAMP)
PHP版本:7.3.9
Symfony版本:Symfony CLI版本v4.16.3
Composer版本:Composer版本1.10.7

问题:

我开始使用Symfony文档中有关测试的提到的PHPUnit,我想通过一个简单的测试来尝试:将get请求发送到我的主页(route ='/'),看看我是否有200状态代码如预期的那样。

我的过程:

在我的项目中:

composer req --dev Symfony/PHPUnit-bridge Symfony/css-selector symfony/browser-kit symfony/maker-bundle
bin/console make:functional-test MainControllerTest

这是tests /中的测试文件:

<?php


namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;


class MainControllerTest extends WebTestCase
{


    public function testIndex()
    {
        $c = static::createClient();
        $c->request('GET','/');
        $this->assertEquals(200, $c->getResponse()->getStatusCode(), 'REQUEST IS : ' . $c->getRequest());
    }
}

这是基本的测试控制器,我只是通过自定义输出消息以将我的请求作为输出进行了一些修改。

然后:

bin/PHPUnit tests

最后是我的输出:

PHPUnit 8.3.5 by Sebastian Bergmann and contributors.

Runtime:       PHP 7.3.8 with Xdebug 2.7.2
Configuration: /Applications/MAMP/htdocs/DEV/symfony_app/phpunit.xml

Testing tests
F                                                                   1 / 1 (100%)

Time: 947 ms, Memory: 24.00 MB

There was 1 failure:

1) App\Tests\Controller\MainControllerTest::testIndex

REQUEST IS : GET /DEV/symfony_app/public/index.php/dossier/all HTTP/1.1
Accept:          text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Charset:  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Language: en-us,en;q=0.5
Host:            localhost:8888
User-Agent:      Symfony BrowserKit
X-Php-Ob-Level:  1


Failed asserting that 404 matches expected 200.

/Applications/MAMP/htdocs/DEV/symfony_app/tests/Controller/MainControllerTest.php:16

FAILURES!
Tests: 1, Assertions: 1, Failures: 1.

但是我的Web服务器正在运行,并且我可以访问“ /”路由,我尝试将Uri更改为“ http:// localhost:8888 / DEV / symfony_app / public /”或“ http:// localhost” :8888 / DEV / symfony_app / public / index.php'
但它总是返回404而不是200。
我已经配置了一些路由,如果您未通过身份验证,则main是其中之一,您将被重定向到“ / login”,这是您可以作为匿名用户访问的仅有路由之一,因此我尝试使用“ /”进行此测试登录”路线,但我也有404状态。

我实际上处于testing documentation的开头,但是如果这个简单的测试不起作用,我将无法继续。
有任何线索吗?

我看到的类似问题:

Symfony functional test fail but the same request works in browser

不幸的是,没有一个答案可以解决我的问题

0 个答案:

没有答案