duanliao6077 2016-09-04 21:20
浏览 34
已采纳

symfony功能测试的500状态代码

I'm performing a test just to see if a certain route is working. The page/route is found at http://localhost/login and when I go there in the browser it works fine and I've tested this with no cookies and my cache cleared.

However, as soon as I test it in the phpunit LoginControllerTest class I recieve a 500 status code internal server error.

<?php

namespace Tests\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class LoginControllerTest extends WebTestCase
{
    public function testLoginPage()
    {
        $client = static::createClient();

        $crawler = $client->request('GET', '/login'); 
        // var_dump($client->getRequest()->getContent()); // Is empty
        // 200 => 'OK'
        $this->assertEquals(200, $client->getResponse()->getStatusCode());
    }
    // ...

And so running phpunit fails with:

Failed asserting that 500 matches expected 200.

Here is the security.yml in case that is of any use in understanding the problem.

# app/config/security.yml
security:
    providers:
        db_provider:
            entity:
                class: AppBundle:Users
                property: Username

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: [ ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH ]

    firewalls:
        secured_area:
            pattern: ^/
            anonymous: ~   

        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        default:
            pattern: ^/manage
            guard:
                authenticators:
                    - app.token_authenticator

    access_control:
        - { path: ^/manage, roles: ROLE_USER }

The route is defined and working (as expected).

// ...
class LoginController extends Controller
{
    /**
     * @Route("/login", name="LoginForm")
     */
    public function loginFormAction(Request $request)
    {
    // ...
  • 写回答

1条回答 默认 最新

  • dpz3471 2016-09-04 21:24
    关注

    The 500 error can be caused by anything in your case.

    What you could do in such situation is to dump the content of the response and check the Symfony debugging message.

    I usually use such code snippet for quick checking such errors directly in terminal:

    if (!$response->isSuccessful()) {
        $block = $crawler->filter('div.text_exception > h1');
        if ($block->count()) {
            $error = $block->text();
        }
    }
    

    where div.text_exception > h1 is the xpath from Symfony2 debug page to the message itself

    Then just print the $error

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题