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

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

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错