Laravel 5.1 PHPunit表单测试每次都失败

时间:2015-11-07 22:04:19

标签: php unit-testing phpunit laravel-5.1

我有一个像这样的测试设置

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

和我正在测试的文件。

{!! 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() !!}

测试会在表单元素名称和slug中的两个变量$ errors上抛出未定义的变量错误(如果没有正确填充某些内容,它们只会点亮表单。)

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

有谁可以告诉为什么以及如何解决这个问题? Laravel标准应该始终存在$ errors变量......

1 个答案:

答案 0 :(得分:2)

  

$ errors变量应始终由Laravel标准出现......

除非您禁用中间件!

通过启用ShareErrorsFromSession来实现该功能;你刚刚禁用它。

Temporarily disable / bypass Middleware的答案在这里有一些指示。