public
Created

Contain Within Matcher for use in your view specs. Faster than css_select and assert_select.

  • Download Gist
contain_within_matcher.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
# Must have Nokogiri gem installed and save this file in your spec/support dir
# Allows you to write cleaner/faster specs in your views (50% faster than css_select)
# Increased readability in your spec doc
# ex.
# rendered.should contain('my heading').within('h1') # searches all H1 tags for 'my heading'
# or
# rendered.should contain('my string') # searches entire rendered string for 'my string'
class Within
def initialize(expected, css_selector)
@expected = expected
@selector = css_selector
end
 
def matches?(actual)
@html = Nokogiri.HTML(actual)
@html.css(@selector).to_s.include?(@expected)
end
 
def failure_message_for_should
"#{@html.to_s} should have located #{@expected} within '#{@selector}'"
end
 
def failure_message_for_should_not
"#{@html.to_s} should not have located #{@expected} within '#{@selector}'"
end
 
def description
"contain #{@expected} within #{@selector}"
end
end
 
 
class Contain
def initialize(expected)
@expected = expected
end
def matches?(actual)
@actual = actual
@actual.include?(@expected)
end
 
def within(selector)
Within.new(@expected, selector)
end
 
def failure_message_for_should
"#{@actual} should contain #{@expected}"
end
 
def failure_message_for_should_not
"#{@actual} should not contain #{@expected}"
end
 
def description
"contain #{@expected}"
end
end
 
def contain(expected)
Contain.new(expected)
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.