【发布时间】:2015-09-24 01:08:57
【问题描述】:
我有一个控制器,我正在尝试对其进行功能测试。
控制器:
<?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:匹配路由“商家报告”。 {"route_parameters":{"_controller":"Zanox\AppBundle\Controller\ReportController::showAction","id":"{id}","_route":"merchant-report"},"request_uri":"@ 987654321@{id}/report?id=1"} []
让您知道 ['id' => 1] 存在于 DB 中。
第一个问题:为什么会失败?
第二个问题:我是否以正确的方式进行功能测试?
【问题讨论】:
-
你检查错误日志了吗?
-
该日志显示尝试请求的 URL 是:localhost/merchant{id}/report?id=1
-
@Ragaolla 可以吗?
标签: php symfony phpunit functional-testing