Created
May 17, 2017 20:37
-
-
Save ptsurbeleu/347a127a5bbdfe904cc88fcd6f9531b4 to your computer and use it in GitHub Desktop.
An example of how to mock external service errors
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
class MyService | |
def send_request(number) | |
# calls an external service and might encounter an error | |
puts "... sending request ..." | |
end | |
def call_me_maybe(number) | |
begin | |
# first attempt | |
self.send_request(number) | |
return true | |
rescue => exception | |
begin | |
# second attempt | |
self.send_request(number) | |
return true | |
rescue => exception | |
return false | |
end | |
end | |
end | |
end | |
test '#call_me_maybe returns true when retry succeeds' do | |
# arrange | |
service = MyService.new() | |
# somehow mock failure on first attempt | |
# to validate the second attempt is tried out | |
# act & assert | |
assert service.call_me_maybe('123-456-7890') | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment