Sha256: 413cdebe0a955033f09d35938c162b141b2fd4cfe97cb548d38adec11f4d589b

Contents?: true

Size: 1.89 KB

Versions: 17

Compression:

Stored size: 1.89 KB

Contents

module ControllerBuilder
  TMP_VIEW_PATH =
    File.expand_path(File.join(TESTAPP_ROOT, 'tmp', 'views')).freeze

  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)
    Rails.application.routes.draw(&block)
    @routes = Rails.application.routes
    class << self
      include ActionDispatch::Assertions
    end
  end

  def build_response(opts = {}, &block)
    action = opts[:action] || 'example'
    partial = opts[:partial] || '_partial'
    block ||= lambda { render :nothing => true }
    controller_class = define_controller('Examples') do
      layout false
      define_method(action, &block)
    end
    controller_class.view_paths = [TMP_VIEW_PATH]

    define_routes do
      match 'examples', :to => "examples##{action}"
    end

    create_view("examples/#{action}.html.erb", "abc")
    create_view("examples/#{partial}.html.erb", "partial")

    @controller = controller_class.new
    @request    = ActionController::TestRequest.new
    @response   = ActionController::TestResponse.new

    class << self
      include ActionController::TestCase::Behavior
    end
    @routes = Rails.application.routes

    get action

    @controller
  end

  def create_view(path, contents)
    full_path = File.join(TMP_VIEW_PATH, path)
    FileUtils.mkdir_p(File.dirname(full_path))
    File.open(full_path, 'w') { |file| file.write(contents) }
  end

  private

  def delete_temporary_views
    FileUtils.rm_rf(TMP_VIEW_PATH)
  end

  def restore_original_routes
    Rails.application.reload_routes!
  end
end

RSpec.configure do |config|
  config.include ControllerBuilder
end

Version data entries

17 entries across 13 versions & 4 rubygems

Version Path
challah-1.0.0.beta vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/controller_builder.rb
challah-0.9.1.beta.3 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/controller_builder.rb
devise_sociable-0.1.0 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/controller_builder.rb
challah-0.9.1.beta vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/controller_builder.rb
challah-0.9.0 vendor/bundle/gems/shoulda-matchers-1.4.2/spec/support/controller_builder.rb
shoulda-matchers-1.4.2 spec/support/controller_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/controller_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/controller_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/controller_builder.rb
challah-rolls-0.2.0 vendor/bundle/gems/challah-0.8.3/vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/controller_builder.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.4.1/spec/support/controller_builder.rb
challah-0.8.3 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/controller_builder.rb
shoulda-matchers-1.4.1 spec/support/controller_builder.rb
shoulda-matchers-1.4.0 spec/support/controller_builder.rb
challah-0.8.1 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/controller_builder.rb
challah-rolls-0.1.0 vendor/bundle/gems/shoulda-matchers-1.3.0/spec/support/controller_builder.rb
shoulda-matchers-1.3.0 spec/support/controller_builder.rb