Created
February 13, 2018 05:47
-
-
Save myronmarston/c619133188f9df451b820fa45b400959 to your computer and use it in GitHub Desktop.
Alternate implementation for https://thomasleecopeland.com/2018/02/12/increment-decrement-matcher.html
This file contains 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
module IncrementAndDecrement | |
def increment(&block) | |
matcher = change(&block).by(1) | |
RSpec::Matchers::AliasedMatcher.new(matcher, lambda do |desc| | |
desc.gsub("changed", "incremented").gsub("change", "increment") | |
end) | |
end | |
def decrement(&block) | |
matcher = change(&block).by(-1) | |
RSpec::Matchers::AliasedMatcher.new(matcher, lambda do |desc| | |
desc.gsub("changed", "decremented").gsub("change", "decrement").gsub("-", "") | |
end) | |
end | |
end | |
RSpec.configure do |c| | |
c.include IncrementAndDecrement | |
end |
Brilliant! That's a big improvement, thank you Myron! Post updated.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Technically,
RSpec::Matchers::AliasedMatchers
isn't part of RSpec's public API, so if you want to stick only with public APIs, you can do:RSpec::Matchers.alias_matcher
is part of the public API, so you don't have to worry about private API breakages with this, but it's also more convoluted, so YMMV.