Test behavior, not implementation

A unit test should check what a function returns (or changes) for a given input, not how it gets there. A good test fails when the behavior breaks and keeps passing through any refactor that leaves the behavior the same.

LLMs tend to write tests that restate the code. They copy its constants, recompute its formulas, and assert which internal methods got called. This makes them brittle to refactors while also being less effective: if a bug in the implementation gets copied into the test, the test can still pass (and maybe even make things harder to fix afterwards!).

Don’t restate the config

If a test reads the same constant or formula as the implementation, it can’t disagree with it. Hard-code expected values instead.

class Order < ApplicationRecord
    FREE_SHIPPING_THRESHOLD = 50
    SHIPPING_RATE_PER_KG = 2

    def shipping_cost
        return 0 if subtotal >= FREE_SHIPPING_THRESHOLD

        weight_kg * SHIPPING_RATE_PER_KG
    end
end
# BAD!
it 'has a free shipping threshold' do
    expect(Order::FREE_SHIPPING_THRESHOLD).to eq(50)
end

it 'charges per kg' do
    order = Order.new(subtotal: 20, weight_kg: 3)
    expect(order.shipping_cost).to eq(3 * Order::SHIPPING_RATE_PER_KG)
end

The first test never calls shipping_cost at all, and the second passes no matter what the rate is set to.

# Good
describe '#shipping_cost' do
    it 'charges by weight just under the threshold' do
        expect(Order.new(subtotal: 49.99, weight_kg: 3).shipping_cost).to eq(6)
    end

    it 'is free at the threshold' do
        expect(Order.new(subtotal: 50, weight_kg: 3).shipping_cost).to eq(0)
    end
end

The same applies to declarations: it { should validate_presence_of(:email) } just restates one line of the model. Test what a caller sees instead (the record doesn’t save and has a useful error), or skip it.

NB: skipping it really is a valid option; not everything demands a test. Useless tests do nothing and burn money and time with CI.

Test the interface and its edges

Test through the public interface that callers actually use. Pick inputs at the edges where behavior changes: thresholds, empty, nil, zero, one, many. The shipping tests above use 49.99 and 50 because changing >= to > only changes the result at exactly 50.

Don’t mirror the implementation with mocks

A common mistake is to stub every call the method makes and then check that each call happened, which repeats the method body line by line.

class Subscription < ApplicationRecord
    def renew!
        update!(expires_at: expires_at + 1.year)
        SubscriptionMailer.renewed(self).deliver_later
    end
end
# BAD!
it 'renews the subscription' do
    subscription = create(:subscription)
    mail = double(deliver_later: true)

    expect(subscription).to receive(:update!).with(expires_at: subscription.expires_at + 1.year)
    expect(SubscriptionMailer).to receive(:renewed).with(subscription).and_return(mail)

    subscription.renew!
end

This breaks if someone rewrites the method (eg self.expires_at += 1.year; save!) even though the behavior is the same. The stubbing also makes the test less deep: this passes even if a validation would make the real save fail.

# Good
it 'extends the expiry by a year and emails the user' do
    subscription = create(:subscription, expires_at: Time.zone.parse('2026-01-15'))

    expect { subscription.renew! }.to have_enqueued_mail(SubscriptionMailer, :renewed)
    expect(subscription.reload.expires_at).to eq(Time.zone.parse('2027-01-15'))
end

Only stub at real boundaries (network, the clock, third-party APIs, etc), then assert on what the function returns or on what it sends across that boundary (eg the amount charged to a card).

# Good
it 'returns nil when the weather API is down' do
    stub_request(:get, /api.weather.example/).to_return(status: 503)
    expect(Forecast.fetch(lat: 37.4, lon: -122.1)).to be_nil
end

Quick checks

  1. Would it fail if the behavior broke? Flip a >= to > and see if anything goes red.
  2. Would it still pass after a refactor that keeps the behavior?
  3. Does the test name describe something a caller cares about?