Behavior tests tell you why the code exists
Key takeaway: Name each test after the requirement it protects, and check outcomes through the public interface instead of mocking the subject.
Name each test after the reason the code exists, and test the outcome instead of the wiring. Those tests survive refactors and tell the next person what they’re about to break.
You’ve probably seen a test like this:
it "cancels the subscription" do
expect(repository).to receive(:find).with(42).and_return(subscription)
expect(subscription).to receive(:update!).with(status: "cancelled")
expect(mailer).to receive(:cancellation_email).with(subscription)
CancelSubscription.new(repository, mailer).call(42)
end
It restates how CancelSubscription works today. Rename update! or move the email to a background job and it fails, even though the customer sees no difference. It also can’t say whether the email is a requirement or a courtesy.
Test the same feature by behavior instead, with names like “stops future renewal charges” and “keeps access until the end of the paid period.” Each test goes through the front door and checks an outcome someone cares about.
The second name is where the why shows up. Someone who simplifies cancellation to revoke access immediately gets a failure that says “keeps access until the end of the paid period,” and learns the delay is deliberate before they ship. Run rspec --format documentation and those names print as a spec a new engineer can read on day one.
Jimmy Bogard put it in one line: “Testing should enable refactoring, not prevent it.” If a refactor breaks forty tests, those tests were checking structure, and the team learns to stop refactoring.
Mocks still belong at the edges, like the payment gateway or email provider. Inside your own code, mock the world, never the subject.
When the requirement changes, you can find the test for the old rule in seconds, since the rule is the test’s name.