Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added radio_group_matcer_spec.rb. [#3 state:resolved]

  • Loading branch information...
commit 34772003168a49a58915dbf2f0a834d57e9fbd0b 1 parent f729ef1
Stephen Anderson authored
View
2  lib/matchers/drop_down_matcher.rb
@@ -2,6 +2,7 @@ module Spec # :nodoc:
module Rails
module Matchers
class DropDownMatcher
+
def initialize target_id, expected
@target_id = target_id
@expected = expected
@@ -24,6 +25,7 @@ def extract_html_content html
doc = Hpricot.XML(html)
doc.search("select##{@target_id}/option").map{|n| n.inner_text.strip}
end
+
end
end
end
View
9 lib/matchers/radio_group_matcher.rb
@@ -2,23 +2,32 @@ module Spec # :nodoc:
module Rails
module Matchers
class RadioGroupMatcher
+
def initialize target_name, expected
@expected = expected
@target_name = target_name
end
+
def matches? response
@actual = extract_html_content response.body
@actual == @expected
end
+
def failure_message
"\nWrong radio group contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
end
+
+ def negative_failure_message
+ "\nShould not have matched radio group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
+ end
+
def extract_html_content html
doc = Hpricot.XML(html)
elements = doc.search('input')
elements = elements.select{|n| n.elem? && n.get_attribute('name') == @target_name}
elements.map{|n| n.get_attribute('value')}
end
+
end
end
end
View
5 lib/matchers/span_text_matcher.rb
@@ -2,22 +2,27 @@ module Spec # :nodoc:
module Rails
module Matchers
class SpanTextMatcher
+
def initialize target_id, expected_text
@xpath = "p span##{target_id}"
@expected = expected_text
end
+
def matches? response
@actual = extract_html_content response.body
@actual == @expected
end
+
def failure_message
"\nWrong span text contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
end
+
def extract_html_content html
doc = Hpricot.XML(html)
elements = doc.search(@xpath)
elements.map{|n| n.inner_text.strip}.first
end
+
end
end
end
View
6 lib/matchers/table_body_matcher.rb
@@ -2,6 +2,7 @@ module Spec # :nodoc:
module Rails
module Matchers
class TableBodyMatcher
+
def initialize table_id_or_expected, expected
case table_id_or_expected
when String
@@ -12,21 +13,26 @@ def initialize table_id_or_expected, expected
end
raise 'Invalid "expected" argument' if @expected.nil?
end
+
def matches? response
@actual = extract_html_content response.body
@actual == @expected
end
+
def failure_message
"\nWrong table body contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n"
end
+
def negative_failure_message
"\nTable body should not have matched: #{@expected.inspect}\n"
end
+
def extract_html_content html
doc = Hpricot.XML(html)
elements = doc.search("table#{"##{@table_id}" if @table_id} tr").select{|e| ! e.search('td').empty? }
elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/\n \t\t/, "\n")}}
end
+
end
end
end
View
2  lib/matchers/table_header_matcher.rb
@@ -2,6 +2,7 @@ module Spec # :nodoc:
module Rails
module Matchers
class TableHeaderMatcher
+
def initialize table_id_or_expected, expected
case table_id_or_expected
when String
@@ -31,6 +32,7 @@ def extract_html_content html
elements = doc.search("table#{"##{@table_id}" if @table_id} tr").select{|e| ! e.search('th').empty? }
elements.map{|n| n.search('/th').map{|n| n.inner_text.strip.gsub(/\n \t\t/, "\n")}}
end
+
end
end
end
View
5 lib/matchers/td_link_matcher.rb
@@ -2,23 +2,28 @@ module Spec # :nodoc:
module Rails
module Matchers
class TdLinkMatcher
+
def initialize target_id, expected_text, expected_link
@xpath = "td##{target_id}"
@expected_link = expected_link
@expected_text = expected_text
end
+
def matches? response
@actual_text = extract_html_content response.body
@actual_text == @expected_text
end
+
def failure_message
"\nWrong #{@element_name} contents.\nexpected: #{@expected_text.inspect}\n found: #{@actual_text.inspect}\n\n"
end
+
def extract_html_content html
doc = Hpricot.XML(html)
elements = doc.search(@xpath)
elements.map{|n| n.inner_text.strip}.first
end
+
end
end
end
View
39 spec/radio_group_matcher_spec.rb
@@ -0,0 +1,39 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+
+describe 'radio_group_matcher' do
+ it 'should find matching radio group' do
+ response = mock_model(Object, :body => '<p><b>Gender</b><br/>M <input id="athlete_gender_m" name="athlete[gender]" type="radio" value="M" /><br/>F <input id="athlete_gender_f" name="athlete[gender]" type="radio" value="F" /></p>')
+ response.should have_radio_group('athlete[gender]', ['M', 'F'])
+ end
+
+ describe 'passed wrong id' do
+ it 'should not match' do
+ response = mock_model(Object, :body => '<p><b>Gender</b><br/>M <input id="athlete_gender_m" name="athlete[gender]" type="radio" value="M" /><br/>F <input id="athlete_gender_f" name="athlete[gender]" type="radio" value="F" /></p>')
+ response.should_not have_radio_group('wrong_id', ['M', 'F'])
+ end
+ end
+
+ describe 'with non-matching expected' do
+ it 'should not match' do
+ response = mock_model(Object, :body => '<p><b>Gender</b><br/>M <input id="athlete_gender_m" name="athlete[gender]" type="radio" value="M" /><br/>F <input id="athlete_gender_f" name="athlete[gender]" type="radio" value="F" /></p>')
+ response.should_not have_radio_group('athlete[gender]', ['M', 'O'])
+ end
+ end
+
+ describe 'with normal failure' do
+ it 'should raise ExpectationNotMetError with correct message' do
+ response = mock_model Object, :body => 'Some non-matching HTML'
+ lambda do
+ response.should have_radio_group('athlete[gender]', ['M', 'F'])
+ end.should raise_error(Spec::Expectations::ExpectationNotMetError, "\nWrong radio group contents.\nexpected: [\"M\", \"F\"]\n found: []\n\n")
+ end
+ end
+
+ describe 'with negative failure' do
+ it 'should raise ExpectationNotMetError' do
+ response = mock_model Object, :body => '<p><b>Gender</b><br/>M <input id="athlete_gender_m" name="athlete[gender]" type="radio" value="M" /><br/>F <input id="athlete_gender_f" name="athlete[gender]" type="radio" value="F" /></p>'
+ lambda{response.should_not have_radio_group('athlete[gender]', ['M', 'F'])}.should raise_error(Spec::Expectations::ExpectationNotMetError,
+ "\nShould not have matched radio group: name: athlete[gender], with: [\"M\", \"F\"]\n\n")
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.