Module: MockServer::Spec::Helpers

Includes:
MockServer::Store
Defined in:
lib/mock_server/spec/helpers.rb

Instance Method Summary collapse

Methods included from MockServer::Store

#mock_server_options_merge, #mock_server_options_read, #mock_server_options_write

Instance Method Details

#mock_server_add_record(*arguments) ⇒ Object

Public: Add fixtures to the list of fixtures

*arguments - Filename…

Examples

mock_server_add_record(:users, :comments)

36
37
38
39
# File 'lib/mock_server/spec/helpers.rb', line 36

def mock_server_add_record(*arguments)
  config = (mock_server_options_fetch(:record_filenames, []) + arguments)
  mock_server_options_set(:record_filenames, config)
end

#mock_server_clear_matchers!Object

Public: Clear all the matchers


168
169
170
# File 'lib/mock_server/spec/helpers.rb', line 168

def mock_server_clear_matchers!
  mock_server_options_set(:matchers, [])
end

#mock_server_delete(path, &block) ⇒ Object

Public: Register a matcher on a DELETE request for a given route.

path - Relative HTTP path to match &block - Optional block for complex matching on the request

Examples

mock_server_delete('/api/2/account')

mock_server_delete('/api/2/account') { |request, recorded_request| request.body == recorded_request.body }

162
163
164
# File 'lib/mock_server/spec/helpers.rb', line 162

def mock_server_delete(path, &block)
  mock_server_request :delete, path, block
end

#mock_server_disable_all_routes!Object

Public: Disable all routes being server by MockServer

Examples

mock_server_disable_all_routes!

87
88
89
# File 'lib/mock_server/spec/helpers.rb', line 87

def mock_server_disable_all_routes!
  mock_server_options_set(:routes, [])
end

#mock_server_disable_path(*paths) ⇒ Object

Public: Disable MockServer on a given routes.

*paths - Sting of the fixtures path

Examples

mock_server_disable_path('/api/2/**', '/api/verify')

75
76
77
78
# File 'lib/mock_server/spec/helpers.rb', line 75

def mock_server_disable_path(*paths)
  routes = mock_server_options_fetch(:routes, []) - paths
  mock_server_options_set(:routes, routes.flatten)
end

#mock_server_enable_routes(*paths) ⇒ Object

Public: Enabled MockServer on a given routes. Accept unix-directory like /* catch all.

*arguments - Sting of the fixtures path

Examples

mock_server_enable_routes('/api/2/**', '/api/verify')

62
63
64
65
# File 'lib/mock_server/spec/helpers.rb', line 62

def mock_server_enable_routes(*paths)
  routes = mock_server_options_fetch(:routes, []) + paths
  mock_server_options_set(:routes, routes)
end

#mock_server_errors_stackObject

Public: Retrive the MockServer request stack.

Return array of request path.


187
188
189
# File 'lib/mock_server/spec/helpers.rb', line 187

def mock_server_errors_stack
  mock_server_options_fetch(:errors_stack, [])
end

#mock_server_errors_stack_clear!Object

Public: Retrive the MockServer errors request stack. i.e.: path being register for being serve by MockServer, but no suitable matcher was found to serve the request will be added to the error stack

Return array of errors.


197
198
199
# File 'lib/mock_server/spec/helpers.rb', line 197

def mock_server_errors_stack_clear!
  mock_server_options_set(:errors_stack, [])
end

#mock_server_get(path, &block) ⇒ Object

Public: Register a matcher on a GET request for a given route.

path - Relative HTTP path to match &block - Optional block for complex matching on the request

Examples

mock_server_get('/api/2/account')

mock_server_get('/api/2/account') { |request, recorded_request| request.body == recorded_request.body }

102
103
104
# File 'lib/mock_server/spec/helpers.rb', line 102

def mock_server_get(path, &block)
  mock_server_request :get, path, block
end

#mock_server_inspectObject

Public: Inspect mock server options

Returns a String.


12
13
14
# File 'lib/mock_server/spec/helpers.rb', line 12

def mock_server_inspect
  self.mock_server_options_read.inspect
end

#mock_server_patch(path, &block) ⇒ Object

Public: Register a matcher on a PATCH request for a given route.

path - Relative HTTP path to match &block - Optional block for complex matching on the request

Examples

mock_server_patch('/api/2/account')

mock_server_patch('/api/2/account') { |request, recorded_request| request.body == recorded_request.body }

147
148
149
# File 'lib/mock_server/spec/helpers.rb', line 147

def mock_server_patch(path, &block)
  mock_server_request :patch, path, block
end

#mock_server_post(path, &block) ⇒ Object

Public: Register a matcher on a POST request for a given route.

path - Relative HTTP path to match &block - Optional block for complex matching on the request

Examples

mock_server_post('/api/2/account')

mock_server_post('/api/2/account') { |request, recorded_request| request.body == recorded_request.body }

117
118
119
# File 'lib/mock_server/spec/helpers.rb', line 117

def mock_server_post(path, &block)
  mock_server_request :post, path, block
end

#mock_server_put(path, &block) ⇒ Object

Public: Register a matcher on a PUT request for a given route.

path - Relative HTTP path to match &block - Optional block for complex matching on the request

Examples

mock_server_put('/api/2/account')

mock_server_put('/api/2/account') { |request, recorded_request| request.body == recorded_request.body }

132
133
134
# File 'lib/mock_server/spec/helpers.rb', line 132

def mock_server_put(path, &block)
  mock_server_request :put, path, block
end

#mock_server_requests_stackObject

Public: Retrive the MockServer request stack.

Return array of request path.


175
176
177
# File 'lib/mock_server/spec/helpers.rb', line 175

def mock_server_requests_stack
  mock_server_options_fetch(:requests_stack, [])
end

#mock_server_requests_stack_clear!Object

Public: Clear the MockServer request stack.


180
181
182
# File 'lib/mock_server/spec/helpers.rb', line 180

def mock_server_requests_stack_clear!
   mock_server_options_set(:requests_stack, [])
end

#mock_server_reraise_matcher_exceptionsObject

Public: Utility helper to reraise errors catch inside the matchers block


243
244
245
246
247
# File 'lib/mock_server/spec/helpers.rb', line 243

def mock_server_reraise_matcher_exceptions
  mock_server_options_fetch(:matcher_exceptions, []).each do |exception|
    raise exception
  end
end

#mock_server_reset!Object

Public: Clear the MockServer state.

alias:

mock_server_response_stack_clear!
mock_server_clear_matchers!
mock_server_disable_all_routes!

235
236
237
238
239
# File 'lib/mock_server/spec/helpers.rb', line 235

def mock_server_reset!
  mock_server_response_stack_clear!
  mock_server_clear_matchers!
  mock_server_disable_all_routes!
end

#mock_server_response_stack_clear!Object

Public: Clear the MockServer response stack.

alias:

mock_server_requests_stack_clear!
mock_server_success_stack_clear!
mock_server_errors_stack_clear!

221
222
223
224
225
# File 'lib/mock_server/spec/helpers.rb', line 221

def mock_server_response_stack_clear!
  mock_server_requests_stack_clear!
  mock_server_success_stack_clear!
  mock_server_errors_stack_clear!
end

#mock_server_set_fixture_path(path) ⇒ Object

Public: Set the path of fixtures files

path - Sting of the fixtures path

Examples

mock_server_set_fixture_path('fixtures/records/')

49
50
51
# File 'lib/mock_server/spec/helpers.rb', line 49

def mock_server_set_fixture_path(path)
  mock_server_options_set(:path, path)
end

#mock_server_success_stackObject

Public: Retrive the MockServer successful request stack.

Return array of successful response stack.


204
205
206
# File 'lib/mock_server/spec/helpers.rb', line 204

def mock_server_success_stack
  mock_server_options_fetch(:success_stack, [])
end

#mock_server_success_stack_clear!Object

Public: Clear the MockServer successful request stack.


209
210
211
# File 'lib/mock_server/spec/helpers.rb', line 209

def mock_server_success_stack_clear!
  mock_server_options_set(:success_stack, [])
end

#mock_server_use_record(*arguments) ⇒ Object

Public: Overwrite or initialize the list of fixtures

*arguments - Filename…

Examples

mock_server_use_record(:users, :comments)

24
25
26
# File 'lib/mock_server/spec/helpers.rb', line 24

def mock_server_use_record(*arguments)
  mock_server_options_set(:record_filenames, arguments)
end