【问题标题】:Select impossible value in select inputs with Symfony DomCrawler使用 Symfony DomCrawler 在选择输入中选择不可能的值
【发布时间】:2014-05-18 00:57:54
【问题描述】:

如果我在表单中的select 输入中发送错误值,我想测试我的应用程序的行为。

这是我的 HTML 表单:

<form (...)>
   (...)
<select name="select_input">
     <option value="1">text</option>
</select>

</select>

在我的测试中,使用爬虫获取表单并尝试“选择”不正确的值:

$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'));

但在控制台中,我收到了消息:

InvalidArgumentException: Input "user_profile[voteProfile]" cannot take "9999999" as a value (possible values: 1)

如何选择不正确的值来测试答案?真实服务器可能会发送不正确的值。

【问题讨论】:

  • 你有没有解决这个问题???如果你是怎么做的???这几天一直困扰着我。 -谢谢
  • 我添加了一个简单的答案,感谢提醒我的问题:-)

标签: php symfony phpunit


【解决方案1】:

disableValidation 函数允许在选项中选择不可能的值:

$form = $crawler->selectButton('button')->form();
$form->get('select_input')->disableValidation()->setValue(999999);

$client->submit($form);

任务完成了!

【讨论】:

    【解决方案2】:

    您的错误消息显示“(可能的值:1)”,它与您的表单中的一个可能值匹配。所以,我真的认为你得到了一个你应该预料到的例外。如果一个真实的服务器要发送一个不正确的值,我怀疑会发生同样的异常。

    在您的测试中,尝试添加注释,如下所示:

    /**
    * @expectedException InvalidArgumentException
    */
    public function testForm()
    {
    
    //test logic here
    
    }
    

    或者:

    public function testForm()
    {
    
    $this->setExpectedException('InvalidArgumentException');
    
    }
    

    更多信息here

    【讨论】:

    • 感谢您的回答,但我并不期待爬虫的异常,而是服务器的回答。我编辑了我的帖子,因为它不明显。
    猜你喜欢
    • 2014-10-30
    • 2018-03-28
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2021-12-09
    • 2019-04-16
    • 1970-01-01
    • 2020-12-09
    相关资源
    最近更新 更多