Last active
May 27, 2020 19:42
-
-
Save danhodge/48ad3a10ab8ba990309c3de34bf8f84b to your computer and use it in GitHub Desktop.
RSpec tips and tricks
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
# Run specs with a given random seed | |
# rspec --seed <seed> | |
# Returning different values from consecutive stub invocations | |
allow(thing).to receive(:message).and_return(1, 2, 3, 4) | |
# Yielding multiple times from a test double | |
allow(thing).to receive(:each).and_yield(1).and_yield(2).and_yield(3).and_yield(4) | |
# Lightweight custom matcher | |
class Matcher | |
def initialize(&match_proc) | |
@match_proc = match_proc | |
end | |
def ==(value) | |
@match_proc.call(value) | |
end | |
end | |
match = Matcher.new do |value| | |
value == expected | |
end | |
allow(thing).to receive(:msg).with(match) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment