Last active
January 18, 2020 15:24
-
-
Save bbozo/fac8a5100d2126a3b7837d1d9231d89b to your computer and use it in GitHub Desktop.
API calls mocking & validation
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
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 |
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 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