简体   繁体   中英

Laravel 5.1 PHPunit form testing fails every time

I have a test set up like this

use WithoutMiddleware;
public function test_it_submits_forms()
{
    $this->visit('/admin/menu/samples')
        ->click('New Sample')
        ->seePageIs('/admin/menu/samples/new/create')
        ->type('test1', 'name')
        ->type('test2','description')
        ->type('test2','introduction')
        ->select(11, 'scenario_id')
        ->type('test','slug')
        ->press('Add');
}

and the file I am testing like this.

{!! Form::open(['url' => 'admin/menu/samples/', 'method' => 'POST']) !!}
<div class="row"><div class="form-group col-xs-8 col-md-7 {!! $errors->has('name') ? 'has-
error' : '' !!}">


    {!! Form::label('name', 'Name:') !!}
    {!! Form::text('name', null,['class' => 'form-control']) !!}
    {!! $errors->first('name', '<span class="help-block">:message</span>')!!}
</div>

<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('introduction', 'Introduction:') !!}
    {!! Form::textarea('introduction',null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('description', 'Description:') !!}
    {!! Form::textarea('description',null, ['class' => 'form-control']) !!}
</div>
<div class="form-group col-xs-8 col-md-7 {!! $errors->has('slug') ? 'has-error' : '' !!}">
    {!! Form::label('slug', 'URL:') !!}
    {!! Form::text('slug',null, ['class' => 'form-control']) !!}
    {!! $errors->first('slug', '<span class="help-block">:message</span>') !!}
</div>
<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('available_from', 'Available from:') !!}
    {!! Form::date('available_from', \Carbon\Carbon::now(), ['class' => 'form-control2']) !!}
</div>
<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('available_until', 'Available until:') !!}
    {!! Form::date('available_until', \Carbon\Carbon::now(), ['class' => 'form-control2']) !!}
</div> 
<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('needs_auth', 'Needs authentication:') !!}
    {!! Form::checkbox('needs_auth', 'value', false) !!}
</div>
<div class="form-group col-xs-8 col-md-7">
    {!! Form::label('is_active', 'Active:') !!}
    {!! Form::checkbox('is_active', 'value', false) !!}
</div>
<div class="form-group col-xs-8 col-md-7">
    <h3>Add a scenario</h3>  
    {!! Form::select('scenario_id',($scenario), null,           
['id'=>'scenario_id','placeholder' => 'Select one...']) !!}

</div>
<div class="form-group col-xs-8 col-md-7">
    {!! Form::submit('Add', ['class' => 'btn btn-primary']) !!}
</div>

{!! Form::close() !!}

The test throws undefined variable errors on both of the variables $errors in form elements name and slug ( they just light up the form if something is not filled properly).

 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/home/vagrant/C...', 9, Array)

Could anyone tell why and how to fix this? The $errors variable should always be there by Laravel standards...

The $errors variable should always be there by Laravel standards...

Unless you disable the middleware!

The feature is implemented by having the ShareErrorsFromSession being enabled; which you just disabled.

The answer at Temporarily disable / bypass Middleware has some pointers form here on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM