Module: Aws::ClientStubs

Included in:
Client
Defined in:
lib/aws-sdk-core/client_stubs.rb

Overview

This module provides the ability to specify the data and/or errors to return when a client is using stubbed responses. Pass ‘:stub_responses => true` to a client constructor to enable this behavior.

Defined Under Namespace

Classes: Stub

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



9
10
11
12
# File 'lib/aws-sdk-core/client_stubs.rb', line 9

def initialize(*args)
  @stubs = {}
  super
end

#next_stub(operation_name) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



85
86
87
88
89
90
91
92
# File 'lib/aws-sdk-core/client_stubs.rb', line 85

def next_stub(operation_name)
  stubs = @stubs[operation_name.to_sym] || []
  case stubs.length
  when 0 then new_stub(operation_name)
  when 1 then stubs.first
  else stubs.shift
  end
end

#stub_responses(operation_name, *stubs) ⇒ void

This method returns an undefined value.

Configures what data / errors should be returned from the named operation when response stubbing is enabled.

## Basic usage

By default, fake responses are generated. You can override the default fake data with specific response data by passing a hash.

# enable response stubbing in the client constructor
client = Aws::S3::Client.new(stub_responses: true)

# specify the response data for #list_buckets
client.stub_responses(:list_buckets, buckets:[{name:'aws-sdk'}])

# no api calls made, stub returned
client.list_buckets.map(&:name)
#=> ['aws-sdk']

## Stubbing Errors

When stubbing is enabled, the SDK will default to generate fake responses with placeholder values. You can override the data returned. You can also specify errors it should raise.

client.stub_responses(:get_object, 'NotFound')
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises Aws::S3::Errors::NotFound

client.stub_responses(:get_object, Timeout::Error)
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises new Timeout::Error

client.stub_responses(:get_object, RuntimeError.new('custom message'))
client.get_object(bucket:'aws-sdk', key:'foo')
#=> raises the given runtime error object

## Stubbing Multiple Responses

Calling an operation multiple times will return similar responses. You can configure multiple stubs and they will be returned in sequence.

client.stub_responses(:head_object, [
  'NotFound',
  { content_length: 150 },
])

client.head_object(bucket:'aws-sdk', key:'foo')
#=> raises Aws::S3::Errors::NotFound

resp = client.head_object(bucket:'aws-sdk', key:'foo')
resp.content_length #=> 150

Parameters:

  • operation_name (Symbol)
  • stubs (Mixed)

    One or more responses to return from the named operation.

Raises:

  • (RuntimeError)

    Raises a runtime error when called on a client that has not enabled response stubbing via ‘:stub_responses => true`.



74
75
76
77
78
79
80
81
82
# File 'lib/aws-sdk-core/client_stubs.rb', line 74

def stub_responses(operation_name, *stubs)
  if config.stub_responses
    apply_stubs(operation_name, stubs.flatten)
  else
    msg = 'stubbing is not enabled; enable stubbing in the constructor '
    msg << 'with `:stub_responses => true`'
    raise msg
  end
end