Laravel 5.1-测试多个选择框 [英] Laravel 5.1 - Testing a multiple selectbox
问题描述
使用Laravel 5.1测试助手为多选框编写测试时,遇到了麻烦.
I'm experiencing trouble when I write my tests for a multiple select box with the Laravel 5.1 testing helpers.
我已经尝试了storeInput和select,这有点像storeInput的别名.当我使用多重选择并且希望将输入格式化为数组时,我已经创建了<select name="roles[]">
,但是这是不可测试的,因此我无法编写类似$this->storeInput( 'roles[]', [ 1, 2 ] )
的内容.我得到的错误是:
I've tried both storeInput and select which is kinda much an alias for storeInput. When I'm working with a multiple select and I want the input to be formatted as an array I've created a <select name="roles[]">
but this is not testable, I can't write something like this $this->storeInput( 'roles[]', [ 1, 2 ] )
. The errors I get then are:
Symfony \ Component \ CssSelector \ Exception \ SyntaxErrorException:预期 标识符或"*",但找到了.
Symfony\Component\CssSelector\Exception\SyntaxErrorException: Expected identifier or "*", but found.`
这怎么可能解决?我也用ID测试过 那么我会收到错误消息无法访问的字段"THE_ID".
How is this possible to go around? I've also tested with an ID but then I get the error `Unreachable field "THE_ID".
推荐答案
我这样做的方法是按以下方式创建storeInput的覆盖(我将其放在基本TestCase中,以便可以通过所有测试来访问它):
The way I did this is to create an override of storeInput as follows (I put this in my base TestCase so I could reach it from all my tests):
public function storeInput($element, $text, $force = false)
{
if ($force) {
$this->inputs[$element] = $text;
return $this;
}
else {
return parent::storeInput($element, $text);
}
}
,然后在测试多选时,删除传递给此方法的标识符的尾括号:
and then when testing a multiselect, strip off the trailing braces for the identifier you pass to this method:
$this->storeInput('roles', [1, 2], true);
这篇关于Laravel 5.1-测试多个选择框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!