PhpUnit测试,如果表单中有多个具有相同名称的复选框,如何选中复选框

时间:2016-08-01 08:57:51

标签: 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">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:没有匹配过滤器[permissions] CSS   查询提供

2 个答案:

答案 0 :(得分:2)

如果在表单和测试中指定了多个复选框的索引,那么它可以正常工作。 形式:

<input id="department_1" name="departments[0]" type="checkbox" value="1">
<input id="department_2" name="departments[1]" type="checkbox" value="2">

单元测试:

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

答案 1 :(得分:1)

我管理这个的唯一方法是:

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