Question

I'm performing some tests on a form with radio buttons. I have a radio button that has the following identifiers:

<input checked=​"checked" class=​"radio_buttons optional" id=​"rating_commute_8" name=​"rating[commute]​" type=​"radio" value=​"8">​

I'm trying to get it to pass the following test, but it is giving me the error Failure/Error: within(:css, '#rating_commute_8').should be_checked LocalJumpError: no block given (yield):

  within(:css, '#rating_commute_8').should be_checked
Was it helpful?

Solution 2

The within method is used for scoping where to look for an element. For example, you might want to only look for the radio button in a certain div:

within(:css, '#some_parent_div') do
  find(:css, '#rating_commute_8').should be_checked
end

In this case, assuming the radio button's id is unique, you do not need to define a specific search scope. You just need to find the element and then check if it is checked. To just find the element, you need to use find instead of within. The following should work:

find(:css, '#rating_commute_8').should be_checked

OTHER TIPS

Newer versions of rspec/capybara without shoulda it would look like this:

within('form') do
  expect(page).to have_checked_field('rating_commute_8')
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top