Class: Spec::Rails::Matchers::TableBodyMatcher
- Inherits:
-
Object
- Object
- Spec::Rails::Matchers::TableBodyMatcher
- Defined in:
- lib/matchers/table_body_matcher.rb
Instance Method Summary collapse
- #extract_html_content(html) ⇒ Object
- #failure_message ⇒ Object
-
#initialize(table_id_or_expected, expected) ⇒ TableBodyMatcher
constructor
A new instance of TableBodyMatcher.
- #matches?(response) ⇒ Boolean
- #negative_failure_message ⇒ Object
Constructor Details
#initialize(table_id_or_expected, expected) ⇒ TableBodyMatcher
Returns a new instance of TableBodyMatcher.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/matchers/table_body_matcher.rb', line 6 def initialize table_id_or_expected, expected case table_id_or_expected when String @table_id = table_id_or_expected @expected = expected when Array @expected = table_id_or_expected end raise 'Invalid "expected" argument' if @expected.nil? end |
Instance Method Details
#extract_html_content(html) ⇒ Object
30 31 32 33 34 |
# File 'lib/matchers/table_body_matcher.rb', line 30 def extract_html_content html doc = Hpricot.XML(html) elements = doc.search("table#{"##{@table_id}" if @table_id} tr").reject{|e| e.search('td').empty? } elements.map{|n| n.search('/td').map{|n| n.inner_text.strip.gsub(/[ \t]*\n[\n \t]*/, "\n")}} end |
#failure_message ⇒ Object
22 23 24 |
# File 'lib/matchers/table_body_matcher.rb', line 22 def "\nWrong table body contents.\nexpected: #{@expected.inspect}\n found: #{@actual.inspect}\n\n" end |
#matches?(response) ⇒ Boolean
17 18 19 20 |
# File 'lib/matchers/table_body_matcher.rb', line 17 def matches? response @actual = extract_html_content response.body @actual == @expected end |
#negative_failure_message ⇒ Object
26 27 28 |
# File 'lib/matchers/table_body_matcher.rb', line 26 def "\nTable body should not have matched: #{@expected.inspect}\n" end |