Skip to content

Instantly share code, notes, and snippets.

@phillbaker
Last active November 1, 2017 17:44
Show Gist options
  • Save phillbaker/7617703 to your computer and use it in GitHub Desktop.
Save phillbaker/7617703 to your computer and use it in GitHub Desktop.
A micro Gem for rspec controller spec default parameters.

RSpec Default Params

When you're testing controllers and need to specify parameters across multiple requests, don't repeat yourself! A micro gem* to wrap up a convenient helper for default parameters in controller specs. Cleaned up version of code in this Stack Overflow answer: http://stackoverflow.com/a/14623960/2284646.

Installation

Add this line to your application's Gemfile:

gem 'rspec-default-params'

And then execute:

$ bundle

Or install it yourself as:

$ gem install rspec-default-params

Make it available to rspec, in your spec/spec_helper.rb:

RSpec.configure do |config|
  config.include(RSpec::DefaultParams, :type => :controller)
end

Usage

In a controller spec, set default_params, for example:

require 'spec_helper'
describe FoosApiController do
  let(:default_params) { { :format => :json } }
  ...

  it "..." do
    ...
    get :index, {} # => implicit format of json
    ...
  end
end

Then no more of this:

get :index, { :format => :json }
get :index, { :format => :json, ... }
put :update, { :format => :json, ... }
post :create, { :format => :json, ... }
delete :destroy, { :format => :json, ... }

License

MIT.

Contributing

Yes, please. This is just a gist, so there are no pull requests, but I'll happily incorporate changes. Fork and modify!

  1. Fork it
  2. Commit your changes (git commit -am 'Add some feature')
  3. Push to your fork
  4. Comment on this gist
Gem::Specification.new do |s|
s.name = 'rspec-default-params'
s.summary = 'Helper for default parameters in controller specs.'
s.description = 'A microgem to reduce duplication in controller specs by providing a mechanism to provide defaults for calling actions on controllers.'
s.version = '0.1.1'
s.license = "MIT"
s.platform = Gem::Platform::RUBY
s.files = ['rspec_default_params.rb']
s.require_path = '.'
s.author = 'phillbaker'
s.email = '[email protected]'
s.homepage = 'https://gist.github.com/phillbaker/7617703'
s.test_file = 'rspec_default_params_spec.rb'
s.add_runtime_dependency('rspec', ["~> 2.8"])
s.add_runtime_dependency('activesupport', [">= 3.0"])
s.add_runtime_dependency('actionpack', [">= 3.0"])
end
# Baesd on http://stackoverflow.com/a/14623960/2284646
require 'active_support/concern'
require 'active_support/core_ext/module/aliasing'
module RSpec
module DefaultParams
extend ActiveSupport::Concern
def process_with_default_params(action, method, parameters={}, session={}, flash=nil)
process_without_default_params(action, method, default_params.merge(parameters || {}), session, flash)
end
included do
let(:default_params) { {} }
alias_method_chain :process, :default_params
end
end
end
require File.expand_path('rspec_default_params')
describe RSpec::DefaultParams do
def process; end
include RSpec::DefaultParams
it "merges default parameters with passed in parameters" do
should_receive(:process_without_default_params).with(:index, :get, { :foo => :bar }, {}, nil)
process_with_default_params(:index, :get, { :foo => :bar })
end
it "provides default parameters" do
should_receive(:process_without_default_params).with(:index, :get, {}, {}, nil)
process_with_default_params(:index, :get)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment