Method: Merb::Test::RequestHelper#mock_request

Defined in:
lib/merb-core/test/helpers/mock_request_helper.rb

#mock_request(path, params = {}, env = {}, &block) ⇒ Object

Deprecated.

A generic request that checks the router for the controller and action. This request goes through the Merb::Router and finishes at the controller.

Parameters

path<String>

The path that should go to the router as the request uri.

params<Hash>

An optional hash that will end up as params in the controller instance.

env<Hash>

An optional hash that is passed to the fake request. Any request options should go here (see fake_request).

&blk

The controller is yielded to the block provided for actions prior to the action being dispatched.

Example

request(path, { :name => 'Homer' }, { :request_method => "PUT" }) do |controller|
  controller.stub!(:current_user).and_return(@user)
end

Notes

Uses Routes.

:api: plugin



330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/merb-core/test/helpers/mock_request_helper.rb', line 330

def mock_request(path, params = {}, env= {}, &block)
  env[:request_method] ||= "GET"
  env[:request_uri], env[:query_string] = path.split('?')
  
  multipart = env.delete(:test_with_multipart)

  request = build_request(params, env)

  opts = check_request_for_route(request) # Check that the request will be routed correctly
  controller_name = (opts[:namespace] ? opts.delete(:namespace) + '/' : '') + opts.delete(:controller)
  klass = Object.full_const_get(controller_name.snake_case.to_const_string)
  
  action = opts.delete(:action).to_s
  params.merge!(opts)

  multipart.nil? ? dispatch_to(klass, action, params, env, &block) : dispatch_multipart_to(klass, action, params, env, &block)
end