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
有帮助吗?

解决方案 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

其他提示

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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top