Skip to content

Instantly share code, notes, and snippets.

@jasonwbarnett
Last active February 13, 2018 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonwbarnett/605a98b4ce29759e57100b7933fd7e1d to your computer and use it in GitHub Desktop.
Save jasonwbarnett/605a98b4ce29759e57100b7933fd7e1d to your computer and use it in GitHub Desktop.
class Jason
def say(x)
puts "Original definition of say... #{x}"
end
end
# expect(Jason).to receive(:say).with(...).and_return(...)
class Jason
alias_method :orig_say, :say
def say(x)
expected_input = 'hello'
if x != expected_input
raise "I expected to receive #{expected_input.inspect}, but I received #{x.inspect} instead!"
else
puts "hello, how are you?"
end
end
end
Jason.new.say "hello"
Jason.new.say "goodbye"
class Jason
def say(x)
puts "Original definition of say... #{x}"
end
end
class Jason
alias_method :orig_say, :say
def say(x)
expected_input = 'hello'
if x != expected_input
orig_say(x)
else
puts "hello, how are you?"
end
end
end
Jason.new.say "hello"
Jason.new.say "goodbye"
class Jason
def say(x)
puts "Original definition of say... #{x}"
end
end
class Jason
alias_method :orig_say, :say
def say(x)
expected_inputs = %w[hello goodbye]
if !expected_inputs.include?(x)
orig_say(x)
elsif x == 'hello'
puts "hello, how are you?"
elsif x == 'goodbye'
puts "goodbye to you, sir!"
end
end
end
Jason.new.say "hello"
Jason.new.say "goodbye"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment