Laravel 5.1 - Testing a multiple selectbox

余生颓废 提交于 2019-12-23 17:24:35

问题


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:


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


来源:https://stackoverflow.com/questions/31399078/laravel-5-1-testing-a-multiple-selectbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!