Laravel 5 PhpUnit测试,如果表单中有多个同名复选框,如何选中复选框
我正在测试一个表单。在表单中,有一些复选框具有相同的名称,因为有多个复选框可供选择 因此,我的复选框如下所示:Laravel 5 PhpUnit测试,如果表单中有多个同名复选框,如何选中复选框,laravel-5,phpunit,Laravel 5,Phpunit,我正在测试一个表单。在表单中,有一些复选框具有相同的名称,因为有多个复选框可供选择 因此,我的复选框如下所示: <div class="col-sm-10"> <div class="checkbox"> <input id="department_1" name="departments[]" type="checkbox" value="1"> <label for="department_1">Sale
<div class="col-sm-10">
<div class="checkbox">
<input id="department_1" name="departments[]" type="checkbox" value="1">
<label for="department_1">Sales</label>
</div>
<div class="checkbox">
<input id="department_2" name="departments[]" type="checkbox" value="2">
<label for="department_2">Marketing</label>
</div>
<div class="checkbox">
<input id="department_3" name="departments[]" type="checkbox" value="3">
<label for="department_3">Tech Help</label>
</div>
</div>
public function testUserCreation()
{
$this->be(User::find(10));
$this->visit('/users/create')
->type('First', 'first_name')
->type('Last', 'last_name')
->type('test@esample.com', 'email')
->type('123456', 'password')
->type('123456', 'password_confirmation')
->check('departments')
->press('Submit')
->seePageIs('/users');
}
当我试图检查是否抛出错误时:
InvalidArgumentException:没有与筛选器[权限]CSS匹配的内容
提供的查询
我处理这件事的唯一方法是:
$this->visit('/users/create')
->submitForm('Submit', [
...
...
'departments[0]' => '1',
'departments[1]' => '2'
])
->seePageIs('/users');
请注意,如果要检查第一项和最后一项,必须按照输入的顺序进行
$this->visit('/users/create')
->submitForm('Submit', [
...
...
'departments[0]' => '1',
'departments[2]' => '3' // index 2 instead 1.
])
->seePageIs('/users');
如果在表单和测试中都为multiple复选框指定了索引,那么它就可以工作了。 表格: 使用命名索引也可以
<input name="departments[department_1]" type="checkbox" value="1">
// [...]
$this->check('departments[department_1]');
// [...]
$this->check('departments[department_1]”);
public function testUserCreation()
{
$this->be(User::find(10));
$this->visit('/users/create')
->type('First', 'first_name')
->type('Last', 'last_name')
->type('test@esample.com', 'email')
->type('123456', 'password')
->type('123456', 'password_confirmation')
->check('departments[0]')
->press('Submit')
->seePageIs('/users');
}
<input name="departments[department_1]" type="checkbox" value="1">
// [...]
$this->check('departments[department_1]');