I'm attempting to use the AWS Ruby SDK response stubs to test some error handling in my interactions with S3.
I've been able to stub a simplistic put_object
case, but for some reason I cannot stub presigned_url
. It's important to note that I am using an older version of the SDK.
# Gemfile
gem 'aws-sdk', '2.3.22'
describe 'Stubbing AWS' do
module S3Helper
def self.presigned_url(object_key:, client:)
resource = ::Aws::S3::Resource.new(:client => client)
bucket = resource.bucket('my-great-bucket')
bucket
.object(object_key)
.presigned_url(:get)
end
end
it 'should allow me to stub signed_url' do
test_client = Aws::S3::Client.new(
:access_key_id => ENV['SECRET_ACCESS_KEY_ID'],
:secret_access_key => ENV['SECRET_ACCESS_KEY'],
:region => ENV['AWS_REGION']
)
test_client.stub_responses(:get_object 'NotFound')
expect do
S3Helper.presigned_url(
object_key: 'my-key',
client: client
)
end.to raise(/NotFound/)
end
end
The test fails because the response does not trigger the NotFound
error, even though I can clearly see that a get_object
request was made (and a fake response was returned) in the application logs.
I'm currently solving around this by passing a mock client to my service, and stubbing out the object
and presigned_url
methods, but I'd prefer to have the SDK do the mocking for me. What am I missing?
I think you are not stubbing the response correctly.
Try this way:
test_client.stub_responses(:get_object, 'NotFound')
From the documentation, I see that the stub_responses
method accepts an array of arguments, not a hash.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.