如何测试textArea的值?
how to test the value of a textArea?
在我的 Symfony 应用程序中,我目前正在按照 documentation 编写我的表单测试。在联系表格中,当数据经过验证时,我会清空表格以供进一步使用。
我想在我的测试中检查这种行为。
我有 4 个字段,其中包含几个我没有在此处表示的约束:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('subject', TextType::class)
->add('email', EmailType::class)
->add('message', TextareaType::class)
;
}
在我的测试中,我在扩展 WebTestCase
:
的 class 中使用了以下函数
public function testContactForm(string $subject, string $name, string $email, string $message, int $nbErrors)
{
$form_name = 'contact';
//do some test, submit etc...
//check that the form is cleaned if valid
if (!$nbErrors) {
$this->assertInputValueSame($form_name . '[subject]', '');
$this->assertInputValueSame($form_name.'[name]', '');
$this->assertInputValueSame($form_name . '[email]', '');
$this->assertInputValueSame($form_name . '[message]', '');
}
}
当然 $this->assertInputValueSame($form_name . '[message]', '');
不起作用,因为 message
是 textArea
。我因此尝试了:
$this->assertSelectorTextContains('#'.$name.'_message', '');
却得到如下错误
mb_strpos(): Empty delimiter
那么在 Symfony 4 中测试 textArea 输入是否为空的好方法是什么?
如果去定义assertInputValueSame
:
public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
), $message);
}
您看到该函数正在寻找 input
分隔符,而 textarea
不是这种情况。相反,您可以使用 :
$this->assertSelectorTextSame('#'.$form_name.'_message', '');
在我的 Symfony 应用程序中,我目前正在按照 documentation 编写我的表单测试。在联系表格中,当数据经过验证时,我会清空表格以供进一步使用。 我想在我的测试中检查这种行为。
我有 4 个字段,其中包含几个我没有在此处表示的约束:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', TextType::class)
->add('subject', TextType::class)
->add('email', EmailType::class)
->add('message', TextareaType::class)
;
}
在我的测试中,我在扩展 WebTestCase
:
public function testContactForm(string $subject, string $name, string $email, string $message, int $nbErrors)
{
$form_name = 'contact';
//do some test, submit etc...
//check that the form is cleaned if valid
if (!$nbErrors) {
$this->assertInputValueSame($form_name . '[subject]', '');
$this->assertInputValueSame($form_name.'[name]', '');
$this->assertInputValueSame($form_name . '[email]', '');
$this->assertInputValueSame($form_name . '[message]', '');
}
}
当然 $this->assertInputValueSame($form_name . '[message]', '');
不起作用,因为 message
是 textArea
。我因此尝试了:
$this->assertSelectorTextContains('#'.$name.'_message', '');
却得到如下错误
mb_strpos(): Empty delimiter
那么在 Symfony 4 中测试 textArea 输入是否为空的好方法是什么?
如果去定义assertInputValueSame
:
public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
{
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
), $message);
}
您看到该函数正在寻找 input
分隔符,而 textarea
不是这种情况。相反,您可以使用 :
$this->assertSelectorTextSame('#'.$form_name.'_message', '');