Skip to content

Instantly share code, notes, and snippets.

@bquorning
Last active November 24, 2016 10:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bquorning/17380163dd0c1aee9dfac10a9495b628 to your computer and use it in GitHub Desktop.
Save bquorning/17380163dd0c1aee9dfac10a9495b628 to your computer and use it in GitHub Desktop.
# Run this script with `ruby rspec-rails-json-controller-params.rb`
# It will spawn two processes: one using Minitest, one using RSpec.
if ARGV.empty?
puts `ruby #{__FILE__} test`
puts `ruby #{__FILE__} spec`
exit
end
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
gem "rails", "5.0.0.1"
gem "minitest", "~> 5.9.1"
gem "rspec-rails", "~> 3.5"
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = File.dirname(__FILE__)
config.session_store :cookie_store, key: "cookie_store_key"
secrets.secret_token = "secret_token"
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
routes.draw do
post "/tests", to: "tests#create"
end
end
class TestsController < ActionController::Base
attr_accessor :boolean_attr
def create
self.boolean_attr = params.fetch(:boolean_attr)
head :ok
end
end
case ARGV.first
when "test"
require "minitest/autorun"
class BugTest < ActionDispatch::IntegrationTest
def test_access_to_boolean_attributes_in_JSON_request
post tests_path, params: { boolean_attr: true }, as: :json
assert_same controller.boolean_attr, true
end
private
def app
Rails.application
end
end
when "spec"
require "rspec/rails"
require "rspec/autorun"
RSpec.describe TestsController, type: :controller do
it "has access to boolean attributes in JSON request" do
post :create, params: { boolean_attr: true }, format: :json
expect(controller.boolean_attr).to be(true)
end
it "works when patching `request` object" do
request.content_type = 'application/json'
post :create, params: { boolean_attr: true }, format: :json
expect(controller.boolean_attr).to be(true)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment