簡體   English   中英

Symfony2 功能測試客戶端請求返回錯誤 500

[英]Symfony2 functional test client request returns error 500

我有一個控制器,我正在嘗試對其進行功能測試。

控制器:

<?php

namespace Zanox\AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route;
use Exception;

/**
 * 
 * @author Mohamed Ragab Dahab <eng.mohamed.dahab@gmail.com>
 * 
 * @Route("merchant")
 * 
 */
class ReportController extends Controller {

    /**
     * Show transaction report regarding to the given merchant ID
     * @author Mohamed Ragab Dahab <eng.mohamed.dahab@gmail.com>
     * @access public
     * 
     * @Route("/{id}/report", name="merchant-report")
     * 
     * @param int $id   Merchant ID
     */
    public function showAction($id) {
        try {
            //Order Service
            $orderService = $this->get('zanox_app.orderService');

            //merchant Orders
            $orders = $orderService->getMerchantOrders($id);

            //render view and pass orders array
            return $this->render('ZanoxAppBundle:Report:show.html.twig', ['orders' => $orders]);
        } catch (Exception $e) {
            //log errors
        }
    }

}

我創建了一個功能測試如下:

namespace Zanox\AppBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class ReportControllerTest extends WebTestCase {

    /**
     * 
     */
    public function testShow() {
        //Client instance
        $client = static::createClient();

        //Act like browsing the merchant listing page, via GET method
        $crawler = $client->request('GET', '/merchant/{id}/report', ['id'=> 1]);

        //Getting the response of the requested URL
        $crawlerResponse = $client->getResponse();

        //Assert that Page is loaded ok
        $this->assertEquals(200, $crawlerResponse->getStatusCode());

        //Assert that the response content contains 'Merchant Listing' text
        $this->assertTrue($crawler->filter('html:contains("Merchant Report")')->count() > 0);
    }

}

但是,此測試失敗,因為第一個斷言返回狀態 500 而不是 200

測試日志顯示:[2015-07-06 21:00:24] request.INFO:匹配路由“merchant-report”。 {"route_parameters":{"_controller":"Zanox\\AppBundle\\Controller\\ReportController::showAction","id":"{id}","_route":"merchant-report"},"request_uri":" http ://localhost/merchant/ {id}/report?id=1"} []

讓您知道 ['id' => 1] 存在於 DB 中。

第一個問題:為什么會失敗?

第二個問題:我是否以正確的方式進行功能測試?

如果您查看日志,您會發現{id}參數沒有被正確替換,而是添加到您的 Uri 的查詢字符串中。 所以嘗試:

$crawler = $client->request('GET', sprintf('/merchant/%d/report', 1));

使用GET ,第三個參數將為 URI 添加查詢參數,使用POST ,將發布這些數據。

至於它失敗的原因 - 您可以通過在測試中執行控制器代碼時使用調試器單步執行控制器代碼來解決問題。 對於您的第二個問題,是的,您正在正確地進行簡單的功能測試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM