Skip to content

Instantly share code, notes, and snippets.

@danhodge
Last active May 27, 2020 19:42
Show Gist options
  • Save danhodge/48ad3a10ab8ba990309c3de34bf8f84b to your computer and use it in GitHub Desktop.
Save danhodge/48ad3a10ab8ba990309c3de34bf8f84b to your computer and use it in GitHub Desktop.
RSpec tips and tricks
# 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