[rspec-users] understanding "within"

Sarah Allen lists at ruby-forum.com
Mon Dec 22 19:03:21 EST 2008

So, I want to test clicking a link in a table and found this handy
example by Aslak:


When /^I delete the (\d+)(?:st|nd|rd|th) lorry$/ do |pos|
  visit lorries_url
  within("table > tr:nth-child(#{pos.to_i+1})") do
    click_link "Destroy"

What if I wanted to destroy the lorry with "name 3" instead of the 3rd
lorry?  I looked up 'within' in the Webrat docs
http://gitrdoc.com/brynary/webrat/tree/master/ but there was no
description and the source is a bit mysterious.

Could someone describe what "table > tr:nth-child(#{pos.to_i+1})" does?
by semantic pattern matching I could guess that "ol >
li:nth-child(#{pos.to_i+1})" would also work, but it would be nifty to
know why.

Posted via http://www.ruby-forum.com/.

More information about the rspec-users mailing list