Class: Spec::Rails::Matchers::ButtonMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(expected_value) ⇒ ButtonMatcher

Returns a new instance of ButtonMatcher.



5
6
7
8
# File 'lib/matchers/button_matcher.rb', line 5

def initialize expected_value
  # @target_id = target_id
  @expected = expected_value
end

Instance Method Details

#extract_html_content(html) ⇒ Object



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

def extract_html_content html
  doc = Hpricot.XML(html)
  # elements = doc.search("//input[@value='#{@expected}' & @type='button|submit']")
  # elements = doc.search("input[@value~='#{@expected}'][@type]")
  elements = doc.search("//input[@value='#{@expected}']").select{|e| ['submit', 'button'].include?(e['type'])}
  elements.map{|n| n['value']}.first
end

#failure_messageObject



15
16
17
# File 'lib/matchers/button_matcher.rb', line 15

def failure_message
  "\nWrong button value.\nexpected: #{@expected.inspect}\n   found: #{@actual.inspect}\n\n"
end

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'lib/matchers/button_matcher.rb', line 10

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

#negative_failure_messageObject



19
20
21
# File 'lib/matchers/button_matcher.rb', line 19

def negative_failure_message
  "\nShould not have matched button: #{@target_id}, with text: '#{@expected}'\n\n"
end