Skip to content
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

Programmable stubs #1120

Closed
danp opened this issue Mar 2, 2016 · 5 comments
Closed

Programmable stubs #1120

danp opened this issue Mar 2, 2016 · 5 comments
Labels
feature-request A feature should be added or improved.

Comments

@danp
Copy link
Contributor

danp commented Mar 2, 2016

It would be great if I could use a callback to determine how to respond to a stubbed request. For example, I want to return an error if a server certificate named foo is deleted but return a successful response for anything else.

@trevorrowe trevorrowe added feature-request A feature should be added or improved. Version 2 labels Mar 2, 2016
@trevorrowe
Copy link
Member

Thanks for the feature request. Depending on what testing framework you use, there are methods to achieve this. For example, using RSpec, you can do the following:

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

# success
allow(client).to receive(:delete_bucket).with(bucket:'aws-sdk').and_return(client.stub_data(:delete_bucket))

# error
allow(client).to receive(:delete_bucket).with(bucket:'aws-sdk-not-empty').and_raise(Aws::S3::Errors::BucketNotEmptyError)

# all other requests will hit the default stubs

Moving this sort of functionality into the client stubbing interface could be helpful as well. The above example can be a bit verbose, and depends on using a mocking/stubbing library. Just sharing it in case it can help.

@danp
Copy link
Contributor Author

danp commented Mar 2, 2016

Thanks for the tip. client.stub_data could help.

A downside to that approach is you lose validation on the names of methods you've stubbed (with allow) and the provided args. For example, I could use that to make delete_buckets(bucket_names: ...) or delete_bucket(bucket_name: ...) work but neither of those are valid for real use.

There are probably ways to use rspec/etc to help prevent that but takes care.

@trevorrowe
Copy link
Member

I agree. It also prevents the SDK from executing the request stack. Response stubbing very carefully only by-passes the network request. This ensures any handlers or plugins still trigger. The rspec #allow method will short-circuit all of these.

@awood45
Copy link
Member

awood45 commented Jun 3, 2016

Moved to the feature request backlog.

@awood45 awood45 closed this as completed Jun 3, 2016
@janko
Copy link
Contributor

janko commented Jun 20, 2017

I think this is already possible by passing a Proc as a stub value. I discovered this feature when reading tests for aws-sdk itself, and I just made a PR to document this: #1539.

To take your original example, you should be able to do something like this:

client.stub_responses(:delete_certificate, -> (context) {
  context.params[:certificate_arn] == "foo" ? "YourError" : {...}
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature-request A feature should be added or improved.
Projects
None yet
Development

No branches or pull requests

4 participants