Class: Spec::Rails::Matchers::DropDownMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(target_id, expected) ⇒ DropDownMatcher

Returns a new instance of DropDownMatcher.



6
7
8
9
# File 'lib/matchers/drop_down_matcher.rb', line 6

def initialize target_id, expected
  @target_id = target_id
  @expected = expected
end

Instance Method Details

#extract_html_content(html) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/matchers/drop_down_matcher.rb', line 28

def extract_html_content html
  doc = Hpricot.XML(html)
  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

#failure_messageObject



16
17
18
19
20
21
22
# File 'lib/matchers/drop_down_matcher.rb', line 16

def failure_message
  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

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
14
# File 'lib/matchers/drop_down_matcher.rb', line 11

def matches? response
  @actual = extract_html_content response.body
  @actual == @expected
end

#negative_failure_messageObject



24
25
26
# File 'lib/matchers/drop_down_matcher.rb', line 24

def negative_failure_message
  "\nShould not have matched dropdown with id: #{@target_id}\n\tand contents: #{@expected.inspect}\n\n"
end