I’m working in a Laravel 11 project using Livewire V3. I have a component called MonitorCreate
component and corresponding class. The class implements the form object. When I submit the form, I get validation errors, but my test doesn’t see any errors, what am I missing?
Component has no errors.
My test
<?php
namespace TestsFeatureLivewireSitePagesMonitors;
use AppLivewireSitePagesMonitorsMonitorCreate;
use IlluminateFoundationTestingRefreshDatabase;
use IlluminateFoundationTestingWithFaker;
use LivewireLivewire;
use TestsTestCase;
class MonitorCreateTest extends TestCase
{
use RefreshDatabase;
public function test_component_can_submit_form_and_see_validation_errors_successfully(): void
{
Livewire::test(MonitorCreate::class)
->call('submit')
->assertHasErrors('form.name');
}
public function test_component_renders_successfully()
{
Livewire::test(MonitorCreate::class)
->assertStatus(200);
}
}
My form object
<?php
namespace AppLivewireSitePagesMonitors;
use AppLivewireSiteFormsMonitorsMonitorForm;
use LivewireAttributesValidate;
use LivewireComponent;
use IlluminateHttpResponse;
use AppModelsMonitor;
class MonitorCreate extends Component
{
public MonitorForm $form;
public function mount()
{
$this->form->responseStatusCodes = collect(array_keys(Response::$statusTexts))->toArray();
}
public function addStatusCode(string $type = 'down', int $code = 200): void
{
$property = 'custom_http_' . $type . '_statuses';
if (in_array($code, $this->form->$property)) {
return;
}
$this->form->$property[] = $code;
}
public function removeStatusCode(string $type = 'down', int $code = 200): void
{
$property = 'custom_http_' . $type . '_statuses';
if (! in_array($code, $this->form->$property)) {
return;
}
$index = array_search($code, $this->form->$property);
if ($index !== false) {
unset($this->form->$property[$index]);
}
}
public function submit()
{
$this->authorize('create', Monitor::class);
$this->form->validate();
$monitor = $this->form->create();
$this->redirect(route('page.account.monitors.index'), navigate: true);
}
public function render()
{
return view('livewire.site.pages.monitors.monitor-create');
}
}