Module: Mock5

Extended by:
Mock5
Included in:
Mock5
Defined in:
lib/mock5.rb,
lib/mock5/api.rb,
lib/mock5/version.rb

Overview

The main module of the gem, exposing all API management methods. Can be included into class.

Defined Under Namespace

Classes: Api

Constant Summary collapse

VERSION =
"1.0.5".freeze

Instance Method Summary collapse

Instance Method Details

#mock(endpoint = nil) { ... } ⇒ Mock5::Api

Generates a new API

Examples:

my_mock_api = Mock5.mock("http://example.com") do
  get "posts" do
    [
      {id: 1, body: "a posy body"},
      {id: 2, body: "another post body"}
    ].to_json
  end

  post "posts" do
    halt 201, "The post was created successfully"
  end
end

Parameters:

  • endpoint (String) (defaults to: nil)

    a url of the API service endpoint to mock. Should only include hostname and schema.

Yields:

  • a block to define behavior using Sinatra API

Returns:



39
40
41
# File 'lib/mock5.rb', line 39

def mock(endpoint=nil, &block)
  Api.new(endpoint, &block)
end

#mount(*apis) ⇒ Set

Mounts given list of APIs. Returns a list of APIs that were actually mounted. The APIs that were already mounted when the method is called are not included in the return value.

Parameters:

  • apis (Enum #to_set)

    a list of APIs to mount

Returns:

  • (Set)

    a list of APIs actually mounted



50
51
52
53
54
55
# File 'lib/mock5.rb', line 50

def mount(*apis)
  apis.to_set.subtract(mounted_apis).each do |api|
    mounted_apis.add api
    registry.register_request_stub api.request_stub
  end
end

#mounted?(*apis) ⇒ Boolean

Returns true if all given APIs are mounted. false otherwise.

Parameters:

  • apis (Enum #to_set)

    a list of APIs to check

Returns:

  • (Boolean)

    true if all given APIs are mounted, false otherwise



76
77
78
# File 'lib/mock5.rb', line 76

def mounted?(*apis)
  apis.to_set.subset?(mounted_apis)
end

#mounted_apisSet

Returns a set of currently mounted APIs

Returns:

  • (Set)

    a list of currently mounted APIs



13
14
15
# File 'lib/mock5.rb', line 13

def mounted_apis
  @_mounted_apis ||= Set.new
end

#unmount(*apis) ⇒ Set

Unmount given APIs. Returns only the list of APIs that were actually unmounted. If the API wasn’t mounted when the method is called, it won’t be included in the return value.

Parameters:

  • apis (Enum #to_set)

    a list of APIs to unmount

Returns:

  • (Set)

    a list of APIs actually unmounted



64
65
66
67
68
69
# File 'lib/mock5.rb', line 64

def unmount(*apis)
  mounted_apis.intersection(apis).each do |api|
    mounted_apis.delete api
    registry.remove_request_stub api.request_stub
  end
end

#unmount_all!Set Also known as: reset!

Unmounts all currently mounted APIs and returns them

Returns:

  • (Set)

    a list of unmounted APIs



117
118
119
# File 'lib/mock5.rb', line 117

def unmount_all!
  unmount *mounted_apis
end

#with_mounted(*apis) { ... } ⇒ Object

Mounts a list of given APIs, executes block and then unmounts them back. Useful for wrapping around RSpec tests. It only unmounts APIs that were not mounted before. Any API that was mounted before the method was called remains mounted.

Examples:

my_api = Mock5.mock("http://example.com") do
  get "index.html" do
    "<h1>Hello world!</h1>"
  end
end

another_api = Mock5.mock("http://foobar.com") do
  get "hello/:what" do
    "<h1>Hello #{params["what"]}</h1>"
  end
end

Mock5.with_mounted my_api, another_api do
  Net::HTTP.get("example.com", "/index.html") # => "<h1>Hello world!</h1>"
  Net::HTTP.get("foobar.com", "/hello/bar") # => "<h1>Hello, bar</h1>"
end

Parameters:

  • apis (Enum #to_set)

    a list of APIs to mount before executing the block

Yields:

  • the block to execute with given APIs being mounted



107
108
109
110
111
112
# File 'lib/mock5.rb', line 107

def with_mounted(*apis)
  mounted = mount(*apis)
  yield
ensure
  unmount *mounted
end