did you figure things out? I am having same issues..
Livewire Testing Error
Hello, I'm starting to develop a web application using Laravel Livewire (Laravel 11.21, Livewire 3.5.6).
Haven't had any issues creating the Livewire components and rendering them onto my pages, however I'm attempting to write some unit tests to verify my functionality but seem to keep running into an error where my component is returning a 404.
My setup;
In my web.php file:
Route::get('/register', [AuthController::class, 'register'])->name(register');
In my AuthController.php:
class AuthController extends Controller
{
...
public function register()
{
return view('auth.register');
}
}
In my auth/register.blade.php file:
<x-layout>
<x-slot:pageTitle>
Register
</x-slot>
<livewire:register-component />
</x-layout>
Then in my views/livewire/register-component.blade.php, I've got a standard registration form which seems to work fine (won't add here).
And in my app/Livewire/RegisterComponent.php:
class RegisterComponent extends Component
{
public $title = 'Register';
public $first_name;
...
public function register()
{
// Do Stuff Here Upon Form Submission
...
}
public function render()
{
return view('livewire.register-component');
}
}
All of the above seems to be working fine. Then my test case (which is where the problem is) - tests/Feature/Livewire/RegisterComponentTest.php:
class RegisterComponentTest extends TestCase
{
// This Test Returns a 404
public function testRendersSuccessfully()
{
Livewire::test(RegisterComponent::class)
->assertStatus(200)
->assertViewIs('livewire.register-component');
}
// This Test Returns `Trying to access array offset on null at vendor/livewire/livewire/src/Mechanisms/HandleComponents/HandleComponents.php:88`
public function testRegister()
{
Livewire::test(RegisterComponent::class)
->call('register')
->assertStatus(200);
}
}
The problem is this the first test is failing because status is 404, rather than 200. If I remove the assertStatus() assertion, and just assert the view, then I get The response is not a view.. And the second test fails because of an array offset error in HandleComponents::update().
Any help would be much appreciated. First time using Livewire so still getting to grips with it a bit.
Please or to participate in this conversation.