79592032

Date: 2025-04-25 07:43:22
Score: 0.5
Natty:
Report link

When Should You Use What?

Use unit tests when you want fast, isolated tests for internal logic.

Use feature tests when testing real-world flows through your app that may depend on routing, middleware, or the database.

Unit Tests

Example:

// tests/Unit/PriceCalculatorTest.php

use Tests\TestCase;

class PriceCalculatorTest extends TestCase
{
    public function test_discount_is_applied_correctly()
    {
        $calculator = new \App\Services\PriceCalculator();
        $result = $calculator->applyDiscount(100, 10); // 10%

        $this->assertEquals(90, $result);
    }
}

Feature Tests

Example

// tests/Feature/UserRegistrationTest.php

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UserRegistrationTest extends TestCase
{
    use RefreshDatabase;

    public function test_user_can_register()
    {
        $response = $this->post('/register', [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'password' => 'password',
            'password_confirmation' => 'password',
        ]);

        $response->assertRedirect('/home');
        $this->assertDatabaseHas('users', ['email' => '[email protected]']);
    }
}
Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Starts with a question (0.5): When
  • Low reputation (1):
Posted by: Talha Rashid