Summer Sale! All accounts are 50% off this week.

BenoitDesrosiers's avatar

view testing returns Undefined variable: errors

I'm trying to do an integration test of a show view.

My test is:

$reponse = $this->call('GET', route('something.show',$something->id)); $reponse->assertViewHasAll(['field1', 'field2']);

But it fails with Facade\Ignition\Exceptions\ViewException: Undefined variable: errors

In the view, I have {{ $errors->first('field1') }} (yes, my show view has $errors because I'm using the same sub-blade for show and edit.

The only way I have found to make the test pass is to protect the view with @if(isset($error)) ....

My question is: why is it working when running it normaly, and failing when testing ?

thanks

0 likes
4 replies
ollie_123's avatar

Can you show us your controller & route?

If i'm understanding your initial post correctly, lets say your page name (located in the view folder) is servicing.blade.php.

Normally in your controller you would have:-

//ServicingController

public function show()
{
    return view('servicing');
// or if its in a folder within the view folder: return view('foldername.your_page_name');
}

Your route would then look something like:-

Route::get('servicing', 'ServicingController@show');

What is it your trying to pass to your view page? Is it something from your database? As this would normally be assigned to a variable and passed into your view. Something like

//ServicingController
use App\Servicing; 

public function show()
{
    $oil = Servicing::all();

    return view('servicing', compact('oil'));
}

Then in your view you would have your fields:-

{{ $oil->name }} // example`
BenoitDesrosiers's avatar

I just found out that in my unit test, I'm using WithoutMiddleware I saw in https://stackoverflow.com/questions/34454081/undefined-variable-errors-in-laravel that the $errror variable is created by the web middleware. So having the WithoutMiddleware is preventing its creation.

So I think this is now solved. ( looked at this issue for 3-4h before posting this question... and found the solution 5 min later ... Murphy's striked again ;) )

thanks

Please or to participate in this conversation.