Phpunit throws an error when testing a form that has a partial look in Laravel

Recently I started writing unit tests, so the concept itself is still not entirely clear to me, I try to study it by writing unit testing in the project I'm working on.

I checked authentication, and all this is good; however, when I want to check the form submission, it returns the following erro:

'ErrorException' with message 'Undefined variable: errors'

When I was debugging, I came across the fact that a variable of the form $errors not defined for Phpunit. The form I'm testing was abstracted with a partial view to reference it for CREATE/UPDATE , but it turns out that Phpunit cannot handle partial views.

I tested it when I comment on the @include('view.name') form that I am testing to pass it.

My question is is there any workaround for this. How can I check partial view views?

Exception

 Caused by exception 'ErrorException' with message 'Undefined variable: errors' in /Users/***/Developpement/***/storage/framework/views/36f0b5638ab33 be6eca70d4986f07ad2:2 Stack trace: #0 /Users/***/Developpement/***/storage/framework/views/36f0b5638ab33 be6eca70d4986f07ad2(2): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Undefined varia...', '/Users/***/D...', 2, Array) #1 /Users/***/Developpement/***/vendor/laravel/framework/src/Illumina te/View/Engines/PhpEngine.php(42): include('/Users/***/D...') #2 /Users/***/Developpement/***/vendor/laravel/framework/src/Illumina te/View/Engines/CompilerEngine.php(58): *** minate\View\Engines\PhpEngine->evaluatePath('/Users/***/D...', Array) #3 /Users/***/Developpement/***/vendor/laravel/framework/src/Illumina te/View/View.php(138): Illuminate\View\Engines\CompilerEngine- >get('/Users/***/D...', Array) 

Parent view

 <div class="container"> {!! Form::open(['route' => 'admin.festivals.create.post', 'enctype' => 'multipart/form-data']) !!} <legend>Create</legend> <div class="pull-right"> <a class="btn btn-info" href=" {{route('admin.festivals.list')}}">Home</a> </div> // HERE IS THE LINE WHEN I COMMENT IT IT WORKS {{ --@include ('admin.festivals._form')--}} {!! Form::submit('Create', ['class' => 'btn btn-primary col-md-2']) !!} {!! Form::close() !!} </div> 

Partial view

 <div class="form-group row {{ $errors->has('start_date') ? 'has-error' : '' }}"> <label class="control-label col col-md-2" for="start_date">Start Date</label> <div class="col col-md-4"> {!! Form::datepicker('start_date', (isset($festival)) ? $festival->start_date : null) !!} {!! $errors->first('start_date', '<span class="help-block">:message</span>') !!} </div> 

+4
source share
1 answer

There is no workaround other than checking with isset() . Unit testing is designed to test internal, not forms.

From wikipedia :

In procedural programming, a unit can be an entire module, but more often it is a separate function or procedure. In object-oriented programming, a unit often represents an entire interface, such as a class, but can be an individual method.

If you want to test the behavior of forms and user interfaces, use a front-end test environment like selenium .

0
source

All Articles