Module: MyApiClient::Stub

Defined in:
lib/my_api_client/rspec/stub.rb

Overview

Test helper module for RSpec

Constant Summary collapse

ERROR_MESSAGE =
'If you use the `raise` option as an error instance, the `response` option ' \
'is ignored. If you want to use both options, you need to specify the ' \
'`raise` option as an error class.'

Instance Method Summary collapse

Instance Method Details

#stub_api_client(klass, **actions_and_options) ⇒ InstanceDouble

Returns a stubbed arbitrary MyApiClient instance.

Examples:

api_client = stub_api_client(
  ExampleApiClient,
  get_users: {                                     # Returns an arbitrary pageable response
    pageable: [ { id: 1 }, { id: 2 }]              # for `#pageable_get`.
  },
  get_user: { response: { id: 1 } },               # Returns an arbitrary response.
  post_users: { id: 1 },                           # You can ommit `response` keyword.
  patch_user: ->(params) { { id: params[:id] } },  # Returns calculated result as response.
  put_user: { raise: MyApiClient::ClientError }    # Raises an arbitrary error.
  delete_user: {
    raise: MyApiClient::ClientError,
    response: { errors: [{ code: 10 }] },          # You can stub response and status code
    status_code: 403,                              # with exception.
  }
)
response = api_client.get_user(id: 123)
response.id # => 1

Parameters:

  • klass (Class)

    Stubbing target class.

  • actions_and_options (Hash)

    Stubbing target method and options

Returns:

  • (InstanceDouble)

    Returns a spy object of the stubbed ApiClient.



70
71
72
73
74
75
# File 'lib/my_api_client/rspec/stub.rb', line 70

def stub_api_client(klass, **actions_and_options)
  instance = instance_double(klass, logger: klass.logger, 'logger=': nil,
                                    error_handlers: klass.error_handlers)
  actions_and_options.each { |action, options| stubbing(instance, action, options) }
  instance
end

#stub_api_client_all(klass, **actions_and_options) ⇒ InstanceDouble

Stubs all instance of arbitrary MyApiClient class. And returns a stubbed arbitrary MyApiClient instance.

Examples:

stub_api_client_all(
  ExampleApiClient,
  get_users: {                                     # Returns an arbitrary pageable response
    pageable: [ { id: 1 }, { id: 2 }]              # for `#pageable_get`.
  },
  get_user: { response: { id: 1 } },               # Returns an arbitrary response.
  post_users: { id: 1 },                           # You can ommit `response` keyword.
  patch_user: ->(params) { { id: params[:id] } },  # Returns calculated result as response.
  put_user: { raise: MyApiClient::ClientError }    # Raises an arbitrary error.
  delete_user: {
    raise: MyApiClient::ClientError,
    response: { errors: [{ code: 10 }] },          # You can stub response and statu code
    status_code: 429,                              # with an arbitrary error.
  }
)
response = ExampleApiClient.new.get_user(id: 123)
response.id # => 1

Parameters:

  • klass (Class)

    Stubbing target class.

  • actions_and_options (Hash)

    Stubbing target method and options

Returns:

  • (InstanceDouble)

    Returns a spy object of the stubbed ApiClient.



38
39
40
41
42
# File 'lib/my_api_client/rspec/stub.rb', line 38

def stub_api_client_all(klass, **actions_and_options)
  instance = stub_api_client(klass, **actions_and_options)
  allow(klass).to receive(:new).and_return(instance)
  instance
end