[英]Laravel 5.1 - Testing a multiple selectbox
I'm experiencing trouble when I write my tests for a multiple select box with the Laravel 5.1 testing helpers. 当我使用Laravel 5.1测试助手为多选框编写测试时,我遇到了麻烦。
I've tried both storeInput and select which is kinda much an alias for storeInput. 我已经尝试了storeInput和select,它有点像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 ] )
. 当我使用多重选择并且我希望输入被格式化为数组时,我创建了一个
<select name="roles[]">
但是这不可测试,我不能写这样的东西$this->storeInput( 'roles[]', [ 1, 2 ] )
。 The errors I get then are: 我得到的错误是:
Symfony\\Component\\CssSelector\\Exception\\SyntaxErrorException: Expected identifier or "*", but found.`
Symfony \\ Component \\ CssSelector \\ Exception \\ SyntaxErrorException:预期的标识符或“*”,但找到了。
How is this possible to go around? 怎么可能到处走? I've also tested with an ID but then I get the error `Unreachable field "THE_ID".
我还测试了一个ID,但后来我得到了错误`Unreachable field“THE_ID”。
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): 我这样做的方法是创建一个storeInput的覆盖,如下所示(我把它放在我的基础TestCase中,所以我可以从我的所有测试中得到它):
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.