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.
- Micro gems as suggested by Jeff Kreeftmeijer (http://jeffkreeftmeijer.com/2011/microgems-five-minute-rubygems)
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
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, ... }
MIT.
Yes, please. This is just a gist, so there are no pull requests, but I'll happily incorporate changes. Fork and modify!
- Fork it
- Commit your changes (
git commit -am 'Add some feature'
) - Push to your fork
- Comment on this gist