Testing is an important part of Laravel and Livewire development. It ensures your application works correctly and prevents future bugs.
🔹 Livewire Testing Methods
➤ assertSet()
Purpose: Checks whether a public property has a specific value.
->assertSet('props', $value);
➤ call()
Purpose: Calls a public method of a Livewire component.
->call('method_name', $params);
➤ set()
Purpose: Sets a new value to a public property.
->set('property', $value);
➤ assertHasErrors()
Purpose: Checks if validation errors exist after calling a method.
->assertHasErrors(['name' => 'required']);
🔹 Database Testing in Laravel
➤ assertDatabaseHas()
Checks if a specific row exists in the database.
$this->assertDatabaseHas('users', [
'name' => 'Hemant'
]);
➤ assertDatabaseMissing()
Checks if a specific row does NOT exist in the database.
$this->assertDatabaseMissing('users', [
'name' => 'Hemant'
]);
🔹 Redirect Testing
->assertRedirect('/post/posts');
After this action, was the user redirected to /post/posts?
🔹 Content Assertion
➤ assertSee()
->assertSee('text');
➤ assertDontSee()
->assertDontSee('Delete');
🔹 Authentication Testing
$user = User::factory()->create([
'role' => 'user',
]);
$this->actingAs($user)
->get('/user/dashboard')
->assertStatus(200);
Creates a test user, logs in as that user, and checks if dashboard loads successfully.
🔹 Livewire Authorization Example
Livewire::actingAs($admin)
->test('admin.users')
->call('deleteUser', $user->id);
$this->assertDatabaseMissing('users', [
'id' => $user->id,
]);
🔹 Testing Login with POST Request
$this->post('/login', [
'email' => $admin->email,
'password' => 'password',
])->assertRedirect('/admin/dashboard');
Sends a POST request to /login, passes email and password, and verifies redirection to the specified URL.