-
-
Save rbxbx/391280 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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