phpunit - URI 的 PHPUnit 测试失败,而请求在浏览器中成功
问题描述
我的应用程序在 Symfony 4.4、PHP 7.4 上。
我的 composer.json :
{
"name": "saro0h/to-do-list",
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"composer/package-versions-deprecated": "^1.11",
"doctrine/annotations": "^1.11",
"doctrine/doctrine-bundle": "^2.1",
"doctrine/doctrine-migrations-bundle": "^3.0",
"doctrine/orm": "^2.7",
"sensio/framework-extra-bundle": "^5.6",
"symfony/apache-pack": "^1.0",
"symfony/asset": "4.4.*",
"symfony/console": "4.4.*",
"symfony/dotenv": "4.4.*",
"symfony/flex": "^1.3.1",
"symfony/form": "4.4.*",
"symfony/framework-bundle": "4.4.*",
"symfony/mailer": "4.4.*",
"symfony/monolog-bundle": "^3.6",
"symfony/security-bundle": "4.4.*",
"symfony/security-csrf": "4.4.*",
"symfony/twig-bundle": "4.4.*",
"symfony/validator": "4.4.*",
"symfony/yaml": "4.4.*"
},
"require-dev": {
"phpstan/phpstan": "^0.12.54",
"symfony/browser-kit": "4.4.*",
"symfony/css-selector": "4.4.*",
"symfony/maker-bundle": "^1.23",
"symfony/phpunit-bridge": "^5.1",
"symfony/stopwatch": "^4.4",
"symfony/web-profiler-bundle": "^4.4"
},
"config": {
"preferred-install": {
"*": "dist"
},
"sort-packages": true
},
"autoload": {
"psr-4": {
"App\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "tests/"
}
},
"replace": {
"paragonie/random_compat": "2.*",
"symfony/polyfill-ctype": "*",
"symfony/polyfill-iconv": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php56": "*"
},
"scripts": {
"auto-scripts": {
"cache:clear": "symfony-cmd",
"assets:install %PUBLIC_DIR%": "symfony-cmd"
},
"post-install-cmd": [
"@auto-scripts"
],
"post-update-cmd": [
"@auto-scripts"
]
},
"conflict": {
"symfony/symfony": "*"
},
"extra": {
"symfony": {
"allow-contrib": false,
"require": "4.4.*"
}
}
}
有我要测试的方法控制器:
<?php
namespace App\Controller;
use App\Entity\User;
use App\Form\UserType;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
class UserController extends AbstractController
{
/**
* list all users.
*
* @Route("/users", name="user_list")
*
* @return Response
*/
public function list(): Response
{
return $this->render('user/list.html.twig', ['users' => $this->getDoctrine()->getRepository(User::class)->findAll()]);
}
}
然后用 PHPUnit 测试它
<?php
namespace App\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
/**
* @internal
* @coversNothing
*/
class UserControllerTest extends WebTestCase
{
/**
* testIndex.
*/
public function testUserList(): void
{
$client = static::createClient();
$client->request('GET', '/users');
$this->assertTrue($client->getResponse()->isSuccessful());
}
}
在浏览器中,请求成功(页面显示无错误,HTTP代码状态为200,profiler中无错误)
但是,当我运行 PHPUnit 时,测试失败并显示 $client->getResponse()->getStatusCode() = 500 !!!!
我该如何解决?
谢谢您的回答
解决方案
我解决了我的问题!
当 a 有了在测试环境中显示页面的想法时,我得到了错误:
驱动程序发生异常:SQLSTATE[HY000] [2002] Aucune connexion n'a pu être établie car l'ordinateur cible l'a expressément refusée。
所以我解决了为测试环境创建数据库的问题,定义了我的.env.test。
推荐阅读
- ceph - Ceph 存储 OSD 磁盘升级(更换更大的驱动器)
- jquery - 如何访问波斯日期选择器 jquery 插件的 onSelect 事件中的 (this) 属性
- python - 将共享相同键的行动态合并为一个
- node.js - Express 中的 NodeJs EHOSTUNREACH
- swift - 这是关于 Publisher.Share 的正常行为吗
- angular - 以具有 *ngIf 的画布 @ViewChild
- laravel - 419 Page Expired while login or register on laravel 7
- video-streaming - Media Foundation H264 硬件编码器中的锯齿/质量问题,尤其是文本(文本上的锯齿和模糊)
- flutter - 如何使用 Dio 在 Flutter 中上传多个图像/文件?
- python - 记录python中类之间的常用方法