Last active
June 9, 2022 08:58
-
-
Save joboccara/c56c4aa2c4107f5625f3cb42f9822f74 to your computer and use it in GitHub Desktop.
Design training: Test and external api for storing images
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
################## TEST ###################### | |
# test/controllers/products_controller_test.rb | |
############################################## | |
test 'gets an image with its details from an external service' do | |
begin | |
ENV['IMAGES_FROM_EXTERNAL_SERVICE'] = 'true' | |
IMAGE_EXTERNAL_ID = 42 | |
ImageExternalService.expects(:upload_image_details).with(width: 800, height: 600).returns(IMAGE_EXTERNAL_ID) | |
ImageExternalService.expects(:get_image_details).with(IMAGE_EXTERNAL_ID).returns({width: 800, height: 600}) | |
image = # Create image with: title: 'Item 2', width: 800, height: 600, source: 'unknown', format: 'jpg' | |
get product_url(image[:id]) | |
res = response.parsed_body | |
assert_equal 'Item 2', res['title'] | |
assert_equal 'image', res['kind'] | |
assert_equal 800, res['width'] | |
ensure | |
ENV.delete('IMAGES_FROM_EXTERNAL_SERVICE') | |
end | |
end | |
########## EXTERNAL SERVICE API ############ | |
# app/repositories/image_external_service.rb | |
############################################ | |
module ImageExternalService | |
def self.get_image_details(id) | |
# You don't need to write this code, it's only used in a test where it's mocked | |
# call external service... | |
# { width: 42, height: 42 } | |
end | |
def self.upload_image_details(width:, height:) | |
# You don't need to write this code, it's only used in a test where it's mocked | |
# call external service... | |
# { id: 42 } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment