public
Created

Items, Widget, and Gears controller spec examples

  • Download Gist
controller_specs.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
# Client required that *all* methods for controllers are covered and
# do not allow guest access.
#
# To remove code duplication, add controller macro, update test suite.
# Macro defaults to :html formatted requests and handles member vs collection (assumes :id)
# Note that we can specify methods, formats, params, etc. along the lines of:
# it_should_block_access_to :index, :format => :json
# it_should_block_access_to :something, :method => :post
# it_should_block_access_to :index, :foo => :bar
 
# spec/support/controller_macros.rb
module ControllerMacros
def self.included(base)
base.extend(ClassMethods)
end
 
module ClassMethods
def default_method_for(action)
case action
when :index, :show, :new, :edit, :export; return :get
when :create; return :post
when :update, :activate; return :put
when :destroy; return :delete
else raise "no default method for #{action}"
end
end
 
def it_should_block_access_to(action, options = {})
options[:format] ||= :html
options[:id] = 1 if [:show, :edit, :destroy, :activate, :update].include?(action)
 
method = options.delete(:method) || default_method_for(action)
 
describe "#{method.to_s.upcase} to #{action} via #{options[:format]}" do
before { send(method, action, options) }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
end
end
end
 
RSpec.configuration.include ControllerMacros, :type => :controller
 
 
# spec/controllers/items_controller_spec.rb
require 'spec_helper'
 
describe ItemsController do
context 'guest' do
it_should_block_access_to :index
it_should_block_access_to :show
it_should_block_access_to :export
it_should_block_access_to :new
it_should_block_access_to :create
it_should_block_access_to :edit
it_should_block_access_to :update
it_should_block_access_to :destroy
it_should_block_access_to :activate
end
end
 
# spec/controllers/widgets_controller_spec.rb
require 'spec_helper'
 
describe WidgetsController do
context 'guest' do
it_should_block_access_to :index
it_should_block_access_to :show
it_should_block_access_to :export
it_should_block_access_to :new
it_should_block_access_to :create
it_should_block_access_to :edit
it_should_block_access_to :update
it_should_block_access_to :destroy
it_should_block_access_to :activate
end
end
 
# spec/controllers/gears_controller_spec.rb
require 'spec_helper'
 
describe GearsController do
context 'guest' do
it_should_block_access_to :index
it_should_block_access_to :show
it_should_block_access_to :export
it_should_block_access_to :new
it_should_block_access_to :create
it_should_block_access_to :edit
it_should_block_access_to :update
it_should_block_access_to :destroy
it_should_block_access_to :activate
end
end
 
# etc.

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.