Skip to content

Instantly share code, notes, and snippets.

@andreypronin
Created October 5, 2014 13:34
Show Gist options
  • Save andreypronin/082da2be82b752dbd89c to your computer and use it in GitHub Desktop.
Save andreypronin/082da2be82b752dbd89c to your computer and use it in GitHub Desktop.
Stubbing AWS in spec_helper. Will also not run tests marked with :live_avs tag.
if ENV.has_key?("STUB_AWS") ||
!ENV.has_key?("AWS_ACCESS_KEY_ID") ||
!ENV.has_key?("AWS_SECRET_ACCESS_KEY")
# Test in the environment where AWS credentials are not provided
require 'aws-sdk'
puts "Stubbing AWS (#{ENV.has_key?("STUB_AWS") ? "forced" : "no keys"})"
RSpec.configure do |c|
c.filter_run_excluding :live_aws
end
AWS.stub! # see https://forums.aws.amazon.com/thread.jspa?threadID=114617
#
# Further capabilities:
# ec2.client.stub_for(:describe_instances)[:reservation_set] = 'my-data'
# resp = ec2.client.describe_instances
# #=> {:reservation_set=>'my-data', :reservation_index=>{}, :instance_index=>{}}
#
# resp1 = ec2.client.new_stub_for(:describe_instances)
# resp2 = ec2.client.new_stub_for(:describe_instances)
# resp1[:called] = 1
# resp2[:called] = 2
# ec2.client.stub(:describe_instances).and_return(resp1, resp2)
# ec2.describe_instances
# #=> {:reservation_set=>[], :reservation_index=>{}, :instance_index=>{}, :call => 1}
# ec2.describe_instances
# #=> {:reservation_set=>[], :reservation_index=>{}, :instance_index=>{}, :call => 2}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment