Using Mocha Minitest, I'm trying to mock an API call (e.g. dynamodb_client.scan() ) that should wait n seconds before returning a response.
The dynamodb scan is a subfunction inside my actual test subject, which is against an entire lambda:
def test_lambda_handler_with_delayed_scan_response
@dynamo_client = Aws::DynamoDB::Client.new(
region: 'mock-region',
access_key_id: 'aws-user',
secret_access_key: 'aws-key'
)
Aws::DynamoDB::Client.stubs(:new).returns(@dynamo_client)
scan_params_1 = {some_key: "some_value"}
response = {code: 200}
@dynamo_client.stubs(:scan).with(scan_params_1).returns(response)
lambda_handler(event: event, context: @context)
# addtl logic asserting the log output of lambda_handler() matches an expected output file
end
What I've tried so far:
@dynamo_client.stubs(:scan).with(scan_params_1).returns(sleep 5; response)
Doesn't work because the sleep is executing during the test setup, instead of during invocation of the method inside lambda_handler
@dynamo_client.stubs(:scan).with(scan_params_1).returns(proc {sleep 5; response })
Doesn't work because the method invocation returns the proc itself, but the proc has to be executed with proc.call, which the lambda_handler would obviously not do.