Skip to content

Instantly share code, notes, and snippets.

@philou
Last active December 22, 2015 19:49
Show Gist options
  • Save philou/6521797 to your computer and use it in GitHub Desktop.
Save philou/6521797 to your computer and use it in GitHub Desktop.
Rspec matchers combinators
# Matcher to verify that all items match something else
RSpec::Matchers.define :all_ do |item_matcher|
match do |actual_items|
actual_items.all? { |item| item_matcher.matches?(item)}
end
description do
"#{item_matcher.description} to be true for all the items"
end
end
# Matcher to verify that at least one item matches something else
RSpec::Matchers.define :have_one_that do |item_matcher|
match do |actual_items|
actual_items.any? { |item| item_matcher.matches?(item)}
end
description do
"#{item_matcher.description} to be true for at least one item"
end
end
# Matcher to verify that an item matches all matchers in a list
RSpec::Matchers.define :and_ do |*matchers|
match do |actual|
matchers.all? {|matcher| matcher.matches?(actual) }
end
description do
(matchers.map &:description).join(" and ")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment