在Symfony2中功能性地测试AJAX/XMLHttpRequest页面

bux*_*bux 16 testing ajax symfony

是否可以在symfony2测试中模拟/生成XMLHttpRequest请求(ajax)?

bux*_*bux 35

在使用"有问题"的答案进行搜索后,正确的语法是:

$crawler = $client->request('GET', '/foo/', array(), array(), array(
    'HTTP_X-Requested-With' => 'XMLHttpRequest',
));
Run Code Online (Sandbox Code Playgroud)


Pro*_*tic 6

Request#isXmlHttpRequest()方法只是检查X-Requested-With标头是否等效XMLHttpRequest.如果这是您用来确定请求是否是ajax调用的方法,那么您可以通过向请求添加适当的标头来模拟测试客户端中的行为:

class FooFunctionalTest extends WebTestCase
{
    $client = static::CreateClient();
    $crawler = $client->request('GET', '/foo/', array(), array(), array(
        'X-Requested-With' => 'XMLHttpRequest',
    ));
    // ...
}
Run Code Online (Sandbox Code Playgroud)

可以在源代码中找到有关Request对象的更多信息.