Laravel 5.1 - Testing a multiple selectbox

2019-07-11 05:23发布

I'm experiencing trouble when I write my tests for a multiple select box with the Laravel 5.1 testing helpers.

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: Expected identifier or "*", but found.`

How is this possible to go around? I've also tested with an ID but then I get the error `Unreachable field "THE_ID".

1条回答
淡お忘
2楼-- · 2019-07-11 05:36

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);
查看更多
登录 后发表回答