thoughtbot--shoulda-matchers/spec/support/controller_builder.rb

75 lines
1.7 KiB
Ruby
Raw Normal View History

2012-04-22 00:21:30 +00:00
module ControllerBuilder
def self.included(example_group)
example_group.class_eval do
after do
delete_temporary_views
restore_original_routes
end
end
end
def define_controller(class_name, &block)
class_name = class_name.to_s
class_name << 'Controller' unless class_name =~ /Controller$/
define_class(class_name, ActionController::Base, &block)
end
def define_routes(&block)
@routes = $test_app.draw_routes(&block)
2012-04-22 00:21:30 +00:00
class << self
include ActionDispatch::Assertions
end
end
def build_response(opts = {}, &block)
action = opts[:action] || 'example'
partial = opts[:partial] || '_partial'
2014-01-17 20:20:44 +00:00
block ||= lambda { render nothing: true }
2012-04-22 00:21:30 +00:00
controller_class = define_controller('Examples') do
layout false
define_method(action, &block)
end
controller_class.view_paths = [ $test_app.temp_views_dir_path ]
2012-04-22 00:21:30 +00:00
define_routes do
2014-01-17 20:20:44 +00:00
get 'examples', to: "examples##{action}"
2012-04-22 00:21:30 +00:00
end
2012-12-20 05:04:27 +00:00
create_view("examples/#{action}.html.erb", 'action')
create_view("examples/#{partial}.html.erb", 'partial')
2012-04-22 00:21:30 +00:00
2012-12-13 17:00:10 +00:00
setup_rails_controller_test(controller_class)
get action
@controller
end
def setup_rails_controller_test(controller_class)
2012-04-22 00:21:30 +00:00
@controller = controller_class.new
2012-12-13 17:00:10 +00:00
@request = ::ActionController::TestRequest.new
@response = ::ActionController::TestResponse.new
2012-04-22 00:21:30 +00:00
class << self
include ActionController::TestCase::Behavior
end
end
def create_view(path, contents)
$test_app.create_temp_view(path, contents)
2012-04-22 00:21:30 +00:00
end
private
def delete_temporary_views
$test_app.delete_temp_views
2012-04-22 00:21:30 +00:00
end
def restore_original_routes
Rails.application.reload_routes!
end
end
RSpec.configure do |config|
config.include ControllerBuilder
end