Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@avit
Forked from mrmemes-eth/have_option_matchers.rb
Created May 5, 2010 20:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avit/391360 to your computer and use it in GitHub Desktop.
Save avit/391360 to your computer and use it in GitHub Desktop.
# usage:
# field_labeled('Select your address').should have_option("Home Address")
Spec::Matchers.define :have_option do |expected|
def options_for(select_field)
select_field.options.map(&:inner_text)
end
match do |select_field|
raise "Field is not a SelectField" unless select_field.kind_of?(Webrat::SelectField)
options_for(select_field).include?(expected)
end
failure_message_for_should do |actual|
"Expected this select field to have the following option: '#{expected}'. Actual options: #{options_for(actual).inspect}"
end
failure_message_for_should_not do |actual|
"Expected the select field not to have the following option: '#{expected}'. It did!"
end
end
# usage:
# field_labeled('Select your address').should have_selected_option("Home Address")
Spec::Matchers.define :have_selected_option do |expected|
def selected_option_for(select_field)
select_field.options.detect do |option|
option.element.attributes.has_key?('selected')
end.inner_text
end
match do |select_field|
raise "Field is not a SelectField" unless select_field.kind_of?(Webrat::SelectField)
selected_option_for(select_field).should == expected
end
failure_message_for_should do |actual|
"Expected '#{expected}' to be selected, but '#{selected_option_for(actual)}' was."
end
failure_message_for_should_not do |actual|
"Expected '#{expected}' to not be selected (but it was)."
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment