Skip to content

Instantly share code, notes, and snippets.

@yosemsweet
Created September 21, 2011 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yosemsweet/1233333 to your computer and use it in GitHub Desktop.
Save yosemsweet/1233333 to your computer and use it in GitHub Desktop.
Capybara matchers in RSpec custom matcher
RSpec::Matchers.define :have_widget_elements_for do |widget|
match do |actual|
puts actual.to_yaml
@errors = {}
expected_elements = {
:widget => ".widget",
:content_type => ".#{widget.content_type}",
:body => ".body",
:title => ".title",
:comment_count => ".comment_count",
:toggle_facebook_comments => "a.toggle_facebook_comments[href='#']",
:toolbox => ".toolbox",
:content => ".content",
:tags => ".tags"
}
error_prefix = "Can't find selector "
expected_elements.each do |key, selector|
puts have_selector(selector).inspect
@errors[key] = error_prefix + selector unless have_selector(selector).matches?(actual)
end
@errors.empty?
end
failure_message_for_should do |actual|
message = "expected #{actual} to have widget elements"
message += "\n#{@errors.to_yaml}" unless @errors.empty?
message
end
failure_message_for_should_not do |actual|
message = "expected #{actual} to not have widget elements"
message += "\n#{@errors.to_yaml}" unless @errors.empty?
message
end
description do
message = "has widget elements for #{widget}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment