Module: Scrivito::TestRequest

Defined in:
lib/scrivito/test_request.rb

Instance Method Summary collapse

Instance Method Details

#for_scrivito_obj(test_obj = nil) ⇒ Object

Make a test request to act as a CMS request. This is necessary for testing controllers that include ControllerActions, because these controllers do not have explicit routes by default.

Examples:

class MyPageControllerTest < ActionController::TestCase
  test 'should respond with success' do
    request.for_scrivito_obj
    get :index
    assert_response :success
  end
end

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/scrivito/test_request.rb', line 29

def for_scrivito_obj(test_obj = nil)
  env[Scrivito::CmsEnv::OBJ_ENV_KEY] = test_obj if test_obj

  class << self
    def assign_parameters(routes, controller_path, action, parameters = {})
      super routes, controller_path, action, parameters
    rescue *ROUTING_ERRORS => e
      begin
        super routes, 'scrivito/cms_dispatch', action, parameters
      rescue *ROUTING_ERRORS
        raise e
      end
    end
  end
end

#for_scrivito_user(test_user) ⇒ Object

Make a test request to act as it’s by a specific Scrivito user

Parameters:



55
56
57
# File 'lib/scrivito/test_request.rb', line 55

def for_scrivito_user(test_user)
  env[EditingContextMiddleware::ENVKEY] = EditingContext.new(editor: -> { test_user })
end