我写过这个简单的测试:
<?php
namespace Hello\ApiBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class HelloControllerTest extends WebTestCase {
public function test() {
$client = static::createClient();
$crawler = $client->request("GET","http://localhost/hello");
$response = $client->getResponse()->getStatusCode();
var_dump($response);
}
}
?>
当我运行此测试时,它会输出404状态代码。
奇怪的是,我没有在ngninx访问日志上看到请求。即使我将URL更改为"/hello"
,它仍然看起来请求未到达本地网络服务器。
如果我只是打开Chrome并正常尝试此网址(http://localhost/hello
),毋庸置疑。
答案 0 :(得分:4)
这是因为Symfony的测试框架实际上只模拟请求(并直接在后面运行调度程序)。它不会发送真实的请求。
在测试应用时,请使用相对路径:
$crawler = $client->request("GET","/hello");