Module: APISmith::WebMockExtensions

Defined in:
lib/api_smith/web_mock_extensions.rb

Overview

A set of extensions to make using APISmith with WebMock (or most test utilities in general) simpler when it comes checking values. Please note this is primarily intended for use with rspec due to dependence on subject in some places.

Author:

  • Darcy Laycock

  • Steve Webb

Instance Method Summary collapse

Instance Method Details

#api_url_for(path) ⇒ String

Expands the given path relative to the API for the current subject class. Namely, this makes it possible to convert a relative path to an endpoint-specified path.

Parameters:

  • path (String)

    the path to expand, minus endpoint etc.

Returns:

  • (String)

    the expanded path



27
28
29
30
31
# File 'lib/api_smith/web_mock_extensions.rb', line 27

def api_url_for(path)
  path     = subject_class_instance.send(:path_for, path)
  base_uri = subject_api_class.base_uri
  File.join base_uri, path
end

#stub_api(type, path) ⇒ Object

Short hand for #stub_request that lets you give it a relative path prior to expanding it.

Parameters:

  • the (:get, :post, :put, :delete)

    verb for the request

  • the (String)

    relative path for the api

Returns:

  • (Object)

    the result from stub_request



37
38
39
# File 'lib/api_smith/web_mock_extensions.rb', line 37

def stub_api(type, path)
  stub_request(type, api_url_for(path))
end

#subject_api_classClass

Returns the class of the current subject

Returns:

  • (Class)

    the subject class



12
13
14
# File 'lib/api_smith/web_mock_extensions.rb', line 12

def subject_api_class
  subject.is_a?(Class) ? subject : subject.class
end

#subject_class_instanceObject

Returns an instance of the subject class, created via allocate (vs. new)

Returns:

  • (Object)

    the instance



18
19
20
# File 'lib/api_smith/web_mock_extensions.rb', line 18

def subject_class_instance
  @subject_class_instance ||= subject_api_class.allocate
end