Class: Spec::Rails::Matchers::ImageMatcher

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

Instance Method Summary collapse

Constructor Details

#initialize(expected_path) ⇒ ImageMatcher

Returns a new instance of ImageMatcher.



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

def initialize expected_path
  @expected = expected_path
end

Instance Method Details

#extract_html_content(html) ⇒ Object



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

def extract_html_content html
  doc = Hpricot.XML(html)
  elements = doc.search("img[@src*=\"#{@expected}\"]")
  # elements.each {|e| puts e.inspect}
  # elements.first.to_html
  elements.map{|n| n.to_html}.first
end

#failure_messageObject



14
15
16
# File 'lib/matchers/image_matcher.rb', line 14

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

#matches?(response) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
12
# File 'lib/matchers/image_matcher.rb', line 9

def matches? response
  @actual = extract_html_content response.body
  @actual =~ /#{@expected}/
end

#negative_failure_messageObject



18
19
20
# File 'lib/matchers/image_matcher.rb', line 18

def negative_failure_message
  "\nShould not have matched image with path: '#{@expected}'\n\n"
end