Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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