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.6".freeze
Instance Method Summary collapse
-
#mock(endpoint = nil) { ... } ⇒ Mock5::Api
Generates a new API.
-
#mount(*apis) ⇒ Set
Mounts given list of APIs.
-
#mounted?(*apis) ⇒ Boolean
Returns true if all given APIs are mounted.
-
#mounted_apis ⇒ Set
Returns a set of currently mounted APIs.
-
#unmount(*apis) ⇒ Set
Unmount given APIs.
-
#unmount_all! ⇒ Set
(also: #reset!)
Unmounts all currently mounted APIs and returns them.
-
#with_mounted(*apis) { ... } ⇒ Object
Mounts a list of given APIs, executes block and then unmounts them back.
Instance Method Details
#mock(endpoint = nil) { ... } ⇒ Mock5::Api
Generates a new API
40 41 42 |
# File 'lib/mock5.rb', line 40 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.
51 52 53 54 55 56 57 |
# File 'lib/mock5.rb', line 51 def mount(*apis) apis.to_set.subtract(mounted_apis).each do |api| check_api 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.
78 79 80 |
# File 'lib/mock5.rb', line 78 def mounted?(*apis) apis.to_set.subset?(mounted_apis) end |
#mounted_apis ⇒ Set
Returns a set of currently mounted APIs
14 15 16 |
# File 'lib/mock5.rb', line 14 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.
66 67 68 69 70 71 |
# File 'lib/mock5.rb', line 66 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
119 120 121 |
# File 'lib/mock5.rb', line 119 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.
109 110 111 112 113 114 |
# File 'lib/mock5.rb', line 109 def with_mounted(*apis) mounted = mount(*apis) yield ensure unmount *mounted end |