Have you try "->withViewErrors" ?
Apr 13, 2022
12
Level 3
"Undefined variable $errors" When Testing A View
Hello,
I am looking at testing a view of mine, just to see if the text I expect to see is there.
$view = $this->view(view: 'admin.users.create', data: [
'roles' => \App\Models\Role::all(),
]);
$view->assertSee(value: ucfirst(string: \App\Enums\RoleName::SuperAdmin->value));
But I am getting an error about an Undefined variable $errors
Here's the whole error form the CLI
• Tests\Feature\Http\Controllers\Admin\UserControllerTest\CreateTest > a "Super Admin" can see all roles
Illuminate\View\ViewException
Undefined variable $errors (View: /Users/chris.mellor/Dev/59club-platform/resources/views/admin/users/create.blade.php)
at storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
14▕ </div>
15▕
16▕ <div class="errors">
17▕ <ul>
➜ 18▕ <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
19▕ <li><?php echo e($error); ?></li>
20▕ <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
21▕ </ul>
22▕ </div>
+1 vendor frames
2 storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
ErrorException::("Undefined variable $errors")
3 storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php:18
Illuminate\Foundation\Bootstrap\HandleExceptions::Illuminate\Foundation\Bootstrap\{closure}("Undefined variable $errors", "/Users/chris.mellor/Dev/59club-platform/storage/framework/views/09c0abaf7e827fb90a41da07901b771f036cbbaa.php")
Yes, the view in question does use the $errors variable as it is checking for validation as the view has a form.
Thought this would be a simple one, but I feel I may have missed something?
Any help is appreciated.
Chris
Level 39
1 like
Please or to participate in this conversation.