Development Workflow

Unit Testing Services and Domain Rules

Verify business rules in isolation with focused unit tests and meaningful boundary cases.

Overview

Unit tests are most valuable when they verify decisions that matter to the application: eligibility, pricing, state transitions, limits and validation rules.

Arrange, act and assert

Set up the smallest valid input, invoke one behavior and assert the result or state change. Test both ordinary cases and boundaries such as zero, maximum values, missing optional data and invalid transitions.

Keep tests independent

A unit test should not depend on execution order, current time, a developer's database or network availability. Use explicit inputs and controlled collaborators.

Assert behavior, not implementation

Prefer assertions about returned values, emitted events or observable collaborator calls. Avoid tests that merely repeat the private steps of the method under test.

Useful test names

Name tests after a business outcome, such as it_rejects_an_expired_invitation. A reader should understand the rule without first opening the production class.

Share this page Help other developers discover this resource.