-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can I use stub_responses with Aws::S3::Resource instead of Aws::S3::Client? #1371
Comments
I was able to get your example to work with a slight change: client_stub = Aws::S3::Client.new(stub_responses: true)
client_stub.stub_responses(:list_buckets, {
buckets: [{ name: 'my-bucket' }]
})
resource_stub = Aws::S3::Resource.new(client: client_stub)
resource_stub.buckets # Note that this won't actually make a stubbed call, but buckets.first works. |
I'll check the documentation to see if we could improve clarity, but let me know if this works for you or if you have further issues with stubbing resources. |
That's helpful and yeah, the documentation is completely unhelpful on this subject, which is a bit of a problem as users of the gem are encouraged more and more to use the My question then becomes: "How could I stub a particular set of objects being returned from a particular bucket?" e.g. I just want to be able to stub that |
So, consider the client calls being made by your resource calls. In this case, you would stub the response to |
i got it to work; would be awesome if you could update the documentation to include this but i'm good for now. |
Thanks for the update, glad things are working well now. We do have an entry for this in the developer guide. Let me know if that would have helped as well (and we can make sure it's more prominently linked to). |
Closing as the issue at hand is resolved, but still interested in your feedback on the guide. |
that part of the developer guide is basically useless for understanding how to stub a |
I agree that that section of the guide was not really helpful when trying to stub when using a Resource instead of a Client. One example that used a Resource would have been very helpful to me. This is what I eventually came up with after seeing the example above:
In hindsight it makes sense that to use "Client Response Stubs" requires using the client, but the documentation did not help me make that mental connection and I slogged through a few dead-ends before I found this thread. Thanks :-) |
Has there been any consideration to providing the ability to stub on resource w/o having to understand the underlying implementation on the client?
What you're saying is that in order to test the API I'm using (Resource), I need to understand how it is implemented in terms of the Client. That feels like an interface that is not fully implemented. |
Ditto DavidRagone's comments. I'm using BUCKETS and perhaps I could stub these but I don't want to have to learn the internals of the AWS SDK in order to do this. I'm guessing that thre is no other simple way to do this though - the example above needs me to know that RESOURCE.instances maps to CLIENT.described_instances[reservations][instances]! Sigh. |
Bump. It's still unclear whether you can stub a resource or not. I found it counter intuitive to create a stubbed client and pass it to the resource, since it can false the specs. I currently do that whenever I want to upload something to S3: Aws::S3::Resource.new(region: region).bucket(bucket).object(file_name).tap do |object|
object.put(body: file_data, **options)
end |
Further bumping this. I'm doing something similar to @erowlin:
And then I'm acting on that object (Specifically in this case calling In my spec I have:
But again, I don't know where to go from here. Do I really need to know the underlying implementation of Probably gonna go for stubbing the methods out with rspec, heh. But that doesn't give me a whole lot of confidence because then I can't be 100% sure that the methods I'm calling or the API I'm relying on is giving me the results I'm expecting. |
Have been wondering the exact same thing as @nzifnab. I see this ticket is closed but I wonder if there is a good answer to this, and if not, I would suggest opening a ticket for it. |
Hello! I'm trying to stub some Resource responses as well and I haven't been able to do so. Is it absolutely necessary to go through the Client? |
I was looking for a way to test my code that relies on AWS S3. I needed to After some digging, I came across these;
If I understand correctly, yes.
https://docs.aws.amazon.com/sdk-for-ruby/v3/api/Aws/S3/Resource.html
If I understand correctly, yes.
Stubbing context 'when the remote object exists' do
it 'builds a blueprint without saving it' do
direct_upload_bucket = class_double(ObjectStorage::DirectUpload::Bucket).as_stubbed_const
remote_object = double(key: 'model.stl', size: 1, content_type: 'model/stl',
public_url: 'http://foo.com/model.stl', exists?: true)
allow(direct_upload_bucket).to receive(:object).and_return(remote_object)
result = service.execute
expect(result).to be_valid
expect(result).not_to be_persisted
end
end Stubbing module StubAWSResponses
def stub_direct_upload_bucket_object_is_not_exist
DIRECT_UPLOAD_RESOURCE.client.stub_responses(:head_object,
status_code: 404,
headers: {},
body: '')
end
def stub_direct_upload_bucket_object_exists
DIRECT_UPLOAD_RESOURCE.client.stub_responses(:head_object,
status_code: 200,
headers: {},
body: '')
end
end Testing response behavior context 'when the remote object is not exist' do
before do
stub_direct_upload_bucket_object_is_not_exist
end
after do
stub_direct_upload_bucket_object_exists # default stub behavior
end
it 'raises a file not found error' do
expect { service.execute }.to raise_error(validation_error, 'File not found')
end
end I made a gist for this Related repository |
I have in the past been able to make very good use of
stub_responses
withAws::S3::Client
; however when it comes toAws::S3::Resource
I am at a bit of a loss as to how to actually stub anything.As an example, I would like to stub a particular key as a response to
Aws::S3::Bucket.objects
. I have managed to get things to work in the sense of not exceptioning out with something likeBut this doesn't work:
and neither does
i tried to create a stub for
Aws::S3::Client
ahead of time with stubbed responses and pass that into theResource
constructor but that also didn't work:and when i tried
i didn't actually get a stubbed response - i got a real response.
The text was updated successfully, but these errors were encountered: