[英]Select impossible value in select inputs with Symfony DomCrawler
I would like to test the behaviour of my application if I send wrong values in a select
input in form. 如果我在表单中的
select
输入中发送错误的值,我想测试我的应用程序的行为。
This is my form in HTML : 这是我在HTML中的表单:
<form (...)>
(...)
<select name="select_input">
<option value="1">text</option>
</select>
</select>
In my test, in get the form with the crawler and try to "select" an incorrect value : 在我的测试中,在使用爬虫获取表单并尝试“选择”不正确的值:
$form['select_input'] = 9999999;
$client->submit($form);
/* EDIT */
/*I am expecting the user to not be redirected to the user page,
and the server to respond with the same form, containing an error message */
$this->assertFalse($client->getResponse()->isRedirect('/success_page'));
$this->assertEquals(1, $client->getCrawler()->filter('input.error'));
But in the console, I get the message : 但在控制台中,我收到消息:
InvalidArgumentException: Input "user_profile[voteProfile]" cannot take "9999999" as a value (possible values: 1)
How can I choose an incorrect value for testing the answer ? 如何选择不正确的值来测试答案? It would be possible to send an incorrect value by a real server.
真实服务器可能会发送错误的值。
The disableValidation
function allow to select impossible values in options : disableValidation
函数允许在选项中选择不可能的值:
$form = $crawler->selectButton('button')->form();
$form->get('select_input')->disableValidation()->setValue(999999);
$client->submit($form);
And the job is done ! 工作完成了!
Your error message says "(possible values: 1)", which matches the one possible value in your form. 您的错误消息显示“(可能的值:1)”,它与表单中的一个可能值匹配。 So, really I think you are getting an exception that you should expect.
所以,我真的认为你得到了一个你应该期待的例外。 If a real server were to send an incorrect value, I would suspect this same exception would occur.
如果真实服务器发送的值不正确,我会怀疑会发生同样的异常。
In your test, try adding an annotation, like this: 在测试中,尝试添加注释,如下所示:
/**
* @expectedException InvalidArgumentException
*/
public function testForm()
{
//test logic here
}
Or: 要么:
public function testForm()
{
$this->setExpectedException('InvalidArgumentException');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.