Last active
December 22, 2015 19:49
-
-
Save philou/6521797 to your computer and use it in GitHub Desktop.
Rspec matchers combinators
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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