skip to content

A Rails toy store applies the wrong discount only to some carts; how do you reproduce it and break only on the failing case?

level: seniorimportance: should knowfreq 28%

answer

  1. reproduce in a test first
  2. debugger behind a Ruby condition
  3. smallest run that reaches it
  4. the whole dev server pauses
  5. keep the test, delete the line

basics

~20 s

Capture a failing cart in a test, then guard the breakpoint with a Ruby condition, such as debugger if line_items.any? { _1.discount_rate.nil? }, in the method that computes the discount. Run that single test so only the bad case stops.

solid answer

~50 s

First make the bug repeatable. Build the failing cart in a model test, with fixtures or the data from the report, and assert the expected discount. Then put a guarded breakpoint in the calculation, `debugger if line_items.any? { _1.discount_rate.nil? }` or whatever separates bad carts, so good carts pass straight through. `debugger` is an ordinary method call, so `if` works on it. Run just that test with `bin/rails test test/models/cart_test.rb:27`, the smallest run that reaches the line. If the bug only appears through the page, the same guarded line works under `bin/rails server`. While it is stopped, the debug gem pauses the server's other threads, so every other request waits too. `console` in the action is the browser alternative. When you have the cause, keep the test as a regression guard and delete the breakpoint, since production does not define `debugger`.

go deeper

for a junior

Recall that debugger is a method call you can guard with if, and that you should delete it before committing.

for a middle

Explain why a guarded breakpoint and a single-test run beat an unconditional one, and that a suspended debugger pauses the whole development server.

for a senior

Show a disciplined loop: capture the case in a test, guard the breakpoint, inspect the calculation's parts, compare the request path, fix, and keep the test.

for a principal

Decide when interactive debugging is the right tool versus more logging or tests, and how the team keeps breakpoints off shared branches.

## Make the bug repeatable first A breakpoint helps only when you can reach the bad state on demand. The toy store's report says "some carts" get the wrong discount, which is not yet a reproduction. Turn it into one: 1. **Find a failing example.** Use the cart id from the report, or the log lines for that checkout, to see what the cart contained. 2. **Encode it in a test.** Build that cart in a model test, with fixtures in a new app or factories if the team uses them, and assert the discount you expect. 3. **Run it.** A red test means you now have a reproduction you can run again in seconds. ```ruby # test/models/cart_test.rb test "bundle discount applies to mixed carts" do cart = carts(:mixed_bundle) # 3 puzzles + 1 gift card assert_equal 1200, cart.discount_total end ``` ## Stop only where the bug is An unconditional `debugger` in `Cart#discount_total` stops for **every** cart. On a page that renders many carts, or in a suite that builds hundreds, you would press continue again and again, and in development the whole server is paused each time. Put the condition in Ruby: ```ruby def discount_total debugger if line_items.any? { _1.discount_rate.nil? } line_items.sum { _1.price * (_1.discount_rate || 0) } end ``` `debugger` is an ordinary method call, so an `if` modifier guards it like any other line. Good carts run straight through, and only the suspicious ones open a prompt. The debug gem also offers conditional breakpoints set from its prompt, but a guarded line needs nothing extra and survives a restart. ## Where to run it | Run | What to know | |---|---| | `bin/rails test test/models/cart_test.rb:27` | The smallest run that reaches the line. The prompt appears in the test terminal. | | A full test run | Large runs are split across parallel workers, which do not share a terminal well. Run the single test instead while a breakpoint is in place. | | The real page under `bin/rails server` | The guarded line stops that request in the server terminal. While the session is suspended, the debug gem pauses the process's other threads too, so other pages wait as well. | | The real page under `bin/dev` with foreman | Set `RUBY_DEBUG_OPEN=true` on the web process and attach from another terminal with `rdbg --attach`. | | No terminal at hand | Put `console` in the action and inspect `@cart` in the browser with web-console. | ## Reading the state At the prompt, evaluate the pieces of the calculation separately: each item's `price`, `discount_rate` and `product`. The usual suspects in a discount bug: - a `nil` rate silently treated as zero; - integer division, where `price * 15 / 100` truncates; - a value that came from `params` and is still a `String`; - an association loaded earlier in the request that holds an out-of-date line item; - a gift card that should be excluded from the discount but is not. When you can name the cause, fix it and let the test from the first step prove it. ## When the test passes but the page is wrong Sometimes the model test is green while the page still shows a wrong total. Then the difference is in the request path, not the model: - the controller builds the cart differently, for example through a scope or `includes` that changes which line items are loaded; - form or query values arrive as strings and are never cast; - a cached fragment shows a total computed before the fix. Put the guarded `debugger` in the controller action, or `console` in the view, and compare the objects there with the ones your test builds. ## Cleaning up - **Delete the breakpoint.** Production does not load the debug gem, so a leftover `debugger` raises `NameError`. The generated omakase RuboCop config disables the `Lint` department, so it will not catch one. - **Keep the test.** It documents the bug and stops it from coming back. ## Takeaway Reproduce before you debug, guard the breakpoint with the condition that separates bad cases from good ones, and run the smallest thing that reaches it. The breakpoint is temporary. The test stays.

  • While one request sits at a debugger prompt in development, can other pages on the same server still load?
    No. When the debug gem suspends, it pauses the other Ruby threads in the process, so Puma's other threads stop too and every other request waits until you continue. This is one more reason to guard the breakpoint with a condition and to remove it as soon as you have your answer.
  • The model test passes, but the cart page still shows the wrong discount. Where do you look next?
    In the request path. The controller may load the cart through a scope or `includes` that differs from the test, values from `params` may still be strings, or a cached fragment may show an old total. Put the guarded `debugger` in the controller action, or `console` in the view, and compare the objects with what the test builds.

  • An unconditional debugger in a shared model method is fine; just keep pressing continue.
  • debugger accepts only a literal line, so you cannot wrap it in an if condition.
  • While one request sits at a debugger prompt, Puma keeps serving other requests normally.
  • The generated RuboCop setup blocks commits that contain debugger calls.
  • Once the bug is fixed, delete the reproducing test along with the breakpoint.