Skip to content

Instantly share code, notes, and snippets.

@bbozo
Last active January 18, 2020 15:24
Show Gist options
  • Save bbozo/fac8a5100d2126a3b7837d1d9231d89b to your computer and use it in GitHub Desktop.
Save bbozo/fac8a5100d2126a3b7837d1d9231d89b to your computer and use it in GitHub Desktop.
API calls mocking & validation
api_exchange << {
request_validator: method(:request_validator).curry[an_option_for_request_validator: true],
response: response_for_valid
}
rv = initiate_authorization
assert_trx_result rv, three_ds_1: TestCaseFaker.test_case
# .....
def request_validator options, request
exp_request = ....
assert_equal_hashes exp_request, act_request, "request doesn't meet expectations"
end
def response_for_valid
<<XML
<?xml version="1.0" encoding="windows-1251"?>
...
XML
end
class FakerForTests
extend ClassFunctional
def self.call request
api_exchange = $faked_api_exchange.shift || {}
raise "undefined response for '#{request}'" unless api_exchange[:response]
request_validator = api_exchange[:request_validator]
response = api_exchange[:response]
request_validator&.call(request)
response
rescue Minitest::Assertion => e
puts "#{e.class}: #{e.message}"
puts e.backtrace.join("\n")
raise(e)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment