Backend Development Laravel Subjective
Sep 30, 2025

Explain Testing strategies in Laravel with code examples.

Detailed Explanation
Laravel provides comprehensive testing capabilities: **Feature Testing:**
class UserTest extends TestCase {
    public function test_user_can_register() {
        $response = $this->post("/register", [
            "name" => "John Doe",
            "email" => "john@example.com",
            "password" => "password",
            "password_confirmation" => "password"
        ]);
        
        $response->assertRedirect("/dashboard");
        $this->assertDatabaseHas("users", [
            "email" => "john@example.com"
        ]);
    }
}
**Unit Testing:**
class UserServiceTest extends TestCase {
    public function test_user_creation() {
        $userService = new UserService();
        $user = $userService->createUser([
            "name" => "Jane Doe",
            "email" => "jane@example.com"
        ]);
        
        $this->assertInstanceOf(User::class, $user);
        $this->assertEquals("Jane Doe", $user->name);
    }
}
**Database Testing:**
use RefreshDatabase;

public function test_user_has_posts() {
    $user = User::factory()->create();
    $posts = Post::factory(3)->create(["user_id" => $user->id]);
    
    $this->assertCount(3, $user->posts);
}
Discussion (0)

No comments yet. Be the first to share your thoughts!

Share Your Thoughts
Feedback