Sha256: d494180cc64049f135b92dbe852778d656c94fdc4ebf5f102f62176a352d8cc9

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

require 'spec_helper'

class DummyController < ActionController::Base
  include ExplicitParameters::Controller

  params do
    accepts :page_size, Integer
    accepts :published, Boolean, default: false
  end
  def index
    render json: {value: params.page_size, type: params.page_size.class.name}
  end

  def error
    param_error!(:page_size, "Out of bound")
    render text: 'OK'
  end

  def no_declaration
    render text: 'OK'
  end

end

RSpec.describe DummyController do
  it 'is optional' do
    get :no_declaration
    expect(response.code).to be == '200'
  end

  it 'coerce parameters to the required type' do
    get :index, page_size: '42'
    expect(json_response).to be == {value: 42, type: 'Fixnum'}
  end

  it 'returns a 422 if parameters are invalid' do
    get :index, page_size: 'foobar'
    expect(response.code).to be == '422'
  end

  it 'returns the list of errors if parameters are invalid' do
    get :index, page_size: 'foobar'
    expect(json_response).to be == {errors: {page_size: ['"foobar" is not a valid Integer']}}
  end

  it 'param_error! interupt the request immediately' do
    get :error
    expect(json_response).to be == {errors: {page_size: ['Out of bound']}}
  end

  private

  attr_reader :response

  def json_response
    JSON.load(response.body).deep_symbolize_keys
  end

  def get(action, parameters = {})
    request(action, 'GET', query: parameters)
  end

  def request(action, method, query: {}, body: '')
    rack_response = subject.dispatch(action, ActionDispatch::Request.new(
      'REQUEST_METHOD' => method,
      'QUERY_STRING' => query.to_query,
      'rack.input' => StringIO.new(body)
    ))
    @response = ActionDispatch::Response.new(*rack_response)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
explicit-parameters-0.1.1 spec/controller_spec.rb
explicit-parameters-0.1.0 spec/controller_spec.rb
explicit-parameters-0.0.3 spec/controller_spec.rb
explicit-parameters-0.0.2 spec/controller_spec.rb