Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Items, Widget, and Gears controller spec examples
# 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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.