#testing

Add binding.pry breakpoint dynamically with pry-byebug

When debugging if you want to check at some lines where you forget to put the binding.pry, you could use some help from pry-byebug’s break points feature.

You can set and adjust breakpoints directly from a Pry session using the break command: https://github.com/deivid-rodriguez/pry-byebug#breakpoints

break SomeClass#run            # Break at the start of `SomeClass#run`.
break app/models/user.rb:15    # Break at line 15 in user.rb.
...

Please bear with me with this example as a showcase.

scenario "Guest can't view draft post" do
  post = create(:post, title: 'Vim', published_at: nil)
  binding.pry
  visit post_path(post)
  expect(page).not_to have_content('Vim')
end

Suppose you put the first binding.pry there to check the creation of post, then somehow you also want to check what’s going on in the controller, normally you would stop/finish the spec and drop the binding.pry to controller and rerun the spec. With pry-byebug you could set the breakpoint in the runtime: when the execution is stopped before the visit post_path(post)

[1] pry(#<RSpec::ExampleGroups::Posts>)> break PostsController#show

  Breakpoint 1: PostsController#show (Enabled)

  10: def show
  11: end

Now the breakpoint is set, if you continue the spec then it’ll stop at the posts#show action.

This is also very useful when you check some codebase you’re not familiar with like gems, during the debugging you also want to check some local variables inside a block or so, with the break command now you don’t need to make changes to the library and restart your rails server.

Qihuan Piao

Qihuan Piao

Originally from China and now living in Tokyo, I started this blog to practice English. Over the years, writing helped me find my own voice and explore thoughts about identity, life, and ordinary things that catch my curiosity. I mostly write for myself, but I love it when something here connects with someone else.

Meanwhile, what I wrote in Japanese

何の論点も結論もない7月1日に描いた文字

July 2, 2016

Meanwhile, what I wrote in Chinese

许久不打台灯

June 9, 2016