Posted about 6 years ago. Visible to the public.

Test that a select field contains an option with Cucumber

This note describes a Cucumber step definition that lets you say:

Copy
Then "Mow lawn" should be an option for "Activity" But "Reply support mail" should not be an option for "Activity"

Note that this step checks whether an option is available, not that it is selected. There is a separate step to test that an option is selected.

Capybara (0.4.1 or higher)

Copy
Then /^"([^"]*)" should( not)? be an option for "([^"]*)"(?: within "([^\"]*)")?$/ do |value, negate, field, selector| with_scope(selector) do expectation = negate ? :should_not : :should field_labeled(field).first(:xpath, ".//option[text() = '#{value}']").send(expectation, be_present) end end

Webrat

Copy
Then /^"([^"]*)" should( not)? be an option for "([^"]*)"$/ do |value, negate, field| expectation = negate ? :should_not : :should field_labeled(field).element.search(".//option[text() = '#{value}']").send(expectation, be_present) end

By refactoring problematic code and creating automated tests, makandra can vastly improve the maintainability of your Rails application.

Author of this card:

Avatar
Henning Koch
Keywords:
cucumber
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandropedia