Class: Spec::Rails::Matchers::TableBodyMatcher

Inherits:
Object
  • Object
show all
Defined in:
lib/matchers/table_body_matcher.rb

Instance Method Summary collapse

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_messageObject



22
23
24
# File 'lib/matchers/table_body_matcher.rb', line 22

def failure_message
  "\nWrong table body contents.\nexpected: #{@expected.inspect}\n   found: #{@actual.inspect}\n\n"
end

#matches?(response) ⇒ Boolean

Returns:

  • (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_messageObject



26
27
28
# File 'lib/matchers/table_body_matcher.rb', line 26

def negative_failure_message
  "\nTable body should not have matched: #{@expected.inspect}\n"
end