maudulus
maudulus

Reputation: 11035

Check that a specific radio button is checked

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

Upvotes: 0

Views: 3501

Answers (2)

dft
dft

Reputation: 648

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

Upvotes: 2

Justin Ko
Justin Ko

Reputation: 46836

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

Upvotes: -1

Related Questions