Sha256: d101a1ae300e998605bf736e22465405ba3dd9077488f220ff40e0c8fa65db56

Contents?: true

Size: 1.19 KB

Versions: 7

Compression:

Stored size: 1.19 KB

Contents

module ControllerHelpers
  def build_controller(name = :controller)
    let(:controller_name) { name }

    let(:controller_class) do
      Class.new(ActionController::Metal).tap do |klass|
        klass.instance_eval do
          def helper_method(*_); end
        end

        klass.send :include, ActionController::Rendering
        klass.send :include, ActionController::Flash
        klass.send :include, AbstractController::Rendering

        klass.send :cattr_accessor, :spec
        klass.spec = self
      end
    end

    let(:request) do
      ActionDispatch::TestRequest.new(ActionDispatch::TestRequest::DEFAULT_ENV.dup).tap do |request|
        request.class_eval do
          def flash
            @env[ActionDispatch::Flash::KEY] ||= ActionDispatch::Flash::FlashHash.new
          end
        end
      end
    end

    let(name) do
      controller_class.new.tap do |controller|
        controller.instance_variable_set(:@_request, request)
      end
    end
  end

  def controller_action(&blk)
    before do
      controller_class.send :define_method, :index do
        instance_eval(&blk)
        render text: ""
      end

      send(controller_name).dispatch(:index, request)
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
warp-1.5.0 spec/support/controller_helpers.rb
warp-1.4.0 spec/support/controller_helpers.rb
warp-1.3.3 spec/support/controller_helpers.rb
warp-1.3.2 spec/support/controller_helpers.rb
warp-1.3.1 spec/support/controller_helpers.rb
warp-1.3.0 spec/support/controller_helpers.rb
warp-1.2.3 spec/support/controller_helpers.rb