Class: Spec::Rails::Matchers::CheckBoxGroupMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(target_name, expected) ⇒ CheckBoxGroupMatcher

Returns a new instance of CheckBoxGroupMatcher.



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

def initialize target_name, expected
  @expected = expected
  @target_name = target_name
end

Instance Method Details

#extract_html_content(html) ⇒ Object



24
25
26
27
28
29
30
# File 'lib/matchers/check_box_group_matcher.rb', line 24

def extract_html_content html
  doc = Hpricot.XML(html)
  # elements = doc.search('div input[type="checkbox"]')
  elements = doc.search('div/input')
  elements = elements.select{|n| n.elem? && n.get_attribute('type') == 'checkbox' && n.get_attribute('name') == @target_name}
  elements.map{|n| value = n.get_attribute('value'); [value, find_label(n)]}
end

#failure_messageObject



16
17
18
# File 'lib/matchers/check_box_group_matcher.rb', line 16

def failure_message
  "\nWrong check box group contents.\nexpected: #{@expected.inspect}\n   found: #{@actual.inspect}\n\n"
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#negative_failure_messageObject



20
21
22
# File 'lib/matchers/check_box_group_matcher.rb', line 20

def negative_failure_message
  "\nShould not have matched check box group: name: #{@target_name}, with: #{@expected.inspect}\n\n"
end