Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More informative failure_message when no select with target_id found.

  • Loading branch information...
commit c3dc79601fdad481001a61aa4eb2a73fd94bf609 1 parent 552361e
Stephen Anderson authored
Showing with 24 additions and 4 deletions.
  1. +13 −2 lib/matchers/drop_down_matcher.rb
  2. +11 −2 spec/drop_down_matcher_spec.rb
View
15 lib/matchers/drop_down_matcher.rb
@@ -14,7 +14,11 @@ def matches? response
end
def failure_message
- "\nWrong drop down contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ if @found_select
+ "\nWrong '#{@target_id}' drop down contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
+ else
+ "\nCould not find a select element with id: '#{@target_id}'\n\n"
+ end
end
def negative_failure_message
@@ -23,7 +27,14 @@ def negative_failure_message
def extract_html_content html
doc = Hpricot.XML(html)
- doc.search("select##{@target_id}/option").map{|n| n.inner_text.strip}
+ select = doc.search("select##{@target_id}")
+ @found_select = ! select.empty?
+
+ if @found_select
+ select.search("/option").map{|n| n.inner_text.strip}
+ else
+ []
+ end
end
end
View
13 spec/drop_down_matcher_spec.rb
@@ -20,12 +20,21 @@
end
end
- describe 'with normal failure' do
+ describe 'without matching select element' do
it 'should raise ExpectationNotMetError with correct message' do
response = mock_model Object, :body => 'Some non-matching HTML'
lambda do
response.should have_dropdown('choice', [['Choice 1', 'Choice 2']])
- end.should raise_error(Spec::Expectations::ExpectationNotMetError, "\nWrong drop down contents.\nexpected: [[\"Choice 1\", \"Choice 2\"]]\n found: []\n\n")
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError, "\nCould not find a select element with id: 'choice'\n\n")
+ end
+ end
+
+ describe 'with wrong contents in correct select element' do
+ it 'should raise ExpectationNotMetError with correct message' do
+ response = mock_model Object, :body => '<select id="choice"><option value="no_match">No Match</option></select>'
+ lambda do
+ response.should have_dropdown('choice', [['Choice 1', 'Choice 2']])
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError, "\nWrong 'choice' drop down contents.\nexpected: [[\"Choice 1\", \"Choice 2\"]]\n found: [\"No Match\"]\n\n")
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.