Aaron
Aaron

Reputation: 14019

Is it possible to stub a file upload using the Ruby AWS SDK?

Going off what I found in the AWS Developers Guide and looking at their API Docs it looks like I should be able to stub upload_file natively. However the following does not work and upload_file simply returns true

client = Aws::S3::Client.new(stub_responses: true)
client.stub_responses(:upload_file, false)

resource = Aws::S3::Resource.new(client: client)
obj = resource.bucket('foo').object('bar')

obj.upload_file(Tempfile.new('tmp')) #=> `true` when I want `false`

The API refers to the first argument of stub_responses as operation_name. The examples often use operation named list_buckets and sometimes head_bucket but I cannot find a list of operations that are acceptable. Am I reading the docs wrong or setting up the example wrong?

Workaround

As a work around I'm using RSpec to stub the method. Assuming I get the same object in obj, the above example just needs the following for my test to pass

allow(obj).to receive(:upload_file).and_return(false)

Upvotes: 3

Views: 1608

Answers (1)

aBadAssCowboy
aBadAssCowboy

Reputation: 2520

After a quick look at the Api docs you pointed, my best guess is that the stub_response is only stubbing the method calls that go directly on client which is an instance of Aws::S3::Client class.

However, you do upload_file on an instance of Aws::S3::Object for which client.stub_reponses wouldn't stub the method.

Upvotes: 1

Related Questions