👉 Go to Summary
There is no wrong or right way, there is our way.
-- Someone
.
.
.
The coverage test trap
- The 100% coverage does not mean anything by itself.
 - You have to be confident.
 - You have to be confident.
 - You have to be confident.
 - Yes, 3 times.
 - Are you really testing what matters? Double check it.
 
Test what matters
- Test only your business rule.
 - You don't need to test what is backed by Laravel.
 - Rule of thumb: if you mutated data you need to test it!
 
// You have a business rule here to test
// You must assure that aged people need assistance, by default!
public function save(Request $request) 
{    
    $data = $request->validate(...);
    if ($data['age'] > 60) {
        $data['need_assistance'] = true;
    }
    User::create($data);
}
// You have a business rule here to test
// After creating an user, you have insert data in another table
public function save(Request $request) 
{    
    $data = $request->validate(...);
    User::create($data);
    Invite::create([something will be created here])
}
Basics
- Get green is not enough, get confident.
 - Test behavior (input/output), not implementation details (mocks).
 - Test’s name is important.
 - Write comments before writing test (GIVEN, WHEN, THEN).
 - Complex features can have more than one test class.
 
// I don't give a shit how avatar was generated!
// I just wanna see the avatar !
class CreateAvatarTest extends TestCase {
    public function test_create_online_user_avatar()
    {
        // GIVEN there is a user
        $user = UserFactory::new()->create();
        // WHEN I request to make an avatar
        $response = $this->get("/avatar-maker?for={$user->id}")
        // THEN I get avatar's URL
        $response->assertJsonFragment(['url' => 'https://...']);
    }    
}
              
    
Top comments (0)