Class: TestProcessor

Inherits:
Object show all
Defined in:
lib/action_profiler/test_processor.rb

Overview

TestProcessor is a class that exercises a Rails controller action.

TestProcessor is heavily based on ActionPack’s lib/action_controller/test_process.rb

The original can be found at: dev.rubyonrails.org/browser/trunk/actionpack/lib/action_controller/test_process.rb

The methods #process and #build_request_uri are copyright © 2004 David Heinemeier Hansson and are used under the MIT License. All original code is subject to the LICENSE file included with Action Profiler. – Per the MIT license:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Direct Known Subclasses

ProfiledProcessor

Instance Method Summary collapse

Constructor Details

#initialize(action, method, params = nil, session = nil, flash = nil) ⇒ TestProcessor

Creates a new TestProcessor that will profile action with method. params, session and flash are hashes for use in processing the request.

action is a String with the format “GamesController#index”.

method is one of the HTTP request methods, get, post, etc.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/action_profiler/test_processor.rb', line 71

def initialize(action, method, params = nil, session = nil, flash = nil)
  unless action =~ /^([:\w]+Controller)#(\w+)$/ then
    raise ArgumentError, "invalid action name" 
  end

  @controller_name = $1
  @action_name = $2
  @method = method.downcase

  @params = params
  @session = session
  @flash = flash

  begin
    controller_klass = Object.path2class @controller_name
  rescue NameError
    raise ArgumentError, "can't find controller #{@controller_name}"
  end

  @controller = controller_klass.new
  @request = ActionController::TestRequest.new
  @response = ActionController::TestResponse.new
  ActionMailer::Base.delivery_method = :test
  ActionMailer::Base.deliveries = []
end

Instance Method Details

#processObject

Runs this action.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/action_profiler/test_processor.rb', line 100

def process
  @request.recycle!

  @html_document = nil # Why? Who knows!
  @request.env['REQUEST_METHOD'] = @method
  @request.action = @action_name

  @request.assign_parameters(@controller.class.controller_path, @action_name,
                             @params)

  @request.session = ActionController::TestSession.new @session
  @request.session['flash'] = ActionController::Flash::FlashHash.new
  @request.session['flash'].update @flash

  build_request_uri
  @controller.process @request, @response
end