我正在为我的项目的铭文部分创建一个功能测试,如果表单需要进入ajax请求,我需要知道如何测试它,否则服务器总是会返回一个空的铭文表单。
看起来提交方法没有使用一个参数来指定它是否是ajax --这与请求方法-> 提交不同
谢谢
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')
);发布于 2015-04-30 03:49:27
请求头中的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',
)
);
}希望能帮上忙
发布于 2016-10-26 09:14:40
实际上,有一种方法可以利用Client::submit,但是如果您想在之后执行非ajax请求,则需要创建新的客户端实例(现在,请参阅下面的GitHub问题链接)。
$client->setServerParameter('HTTP_X-Requested-With', 'XMLHttpRequest');
$client->submit($form);
// The following method doesn't exist yet.
// @see https://github.com/symfony/symfony/issues/20306
// If this method gets added then you won't need to create
// new Client instances for following non-ajax requests,
// you can just do this:
// $client->unsetServerParameter('HTTP_X-Requested-With');https://stackoverflow.com/questions/29956053
复制相似问题