在功能测试中使用ajax提交表单

sma*_*ber 4 php ajax phpunit functional-testing symfony

我正在为项目的题字部分创建功能测试,如果表单需要进入ajax请求,我需要知道如何测试它,否则服务器将始终返回空的题字表单.

看起来提交方法没有采用一个参数来指定它是否是一个不像请求方法的请求的ajax - > http://api.symfony.com/2.3/Symfony/Component/HttpKernel/Client.html#method_submit

谢谢

UPDATE1

////////////////////////////////////////////////
// My functional test looks exactly like this //
////////////////////////////////////////////////
$form = $buttonCrawlerNode->form(array(
    'name'              => 'Fabien',
    'my_form[subject]'  => 'Symfony rocks!',
));
// There is no way here I can tell client to submit using ajax!!!!
$client->submit($form);

// Why can't we tell client to submit using ajax???
// Like we do here in the request méthod
$client->request(
    'GET',
    '/post/hello-world',
    array(),
    array(),
    array('HTTP_X-Requested-With' => 'XMLHttpRequest')
);
Run Code Online (Sandbox Code Playgroud)

Mat*_*teo 7

Symfony 请求对象拦截请求标头中的XmlHttpRequest.因此,只需在测试类中为您的请求添加正确的标头,例如:

class FooFunctionalTest extends WebTestCase
{
    $client = static::CreateClient();
    $url = '/post/hello-world';
    // makes the POST request
    $crawler = $client->request('POST', $url, array(
        'my_form' => array(
            'subject' => 'Symfony rocks!'
        )),
        array(),
        array(
            'HTTP_X-Requested-With' => 'XMLHttpRequest',
        )
    );
}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助