Last active
February 13, 2018 17:26
-
-
Save jasonwbarnett/605a98b4ce29759e57100b7933fd7e1d to your computer and use it in GitHub Desktop.
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
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" |
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
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" |
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
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