Skip to content

Instantly share code, notes, and snippets.

@jeffreyiacono
Created August 9, 2011 15:00
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 jeffreyiacono/1134284 to your computer and use it in GitHub Desktop.
Save jeffreyiacono/1134284 to your computer and use it in GitHub Desktop.
Items, Widget, and Gears controller spec examples
# Client required that *all* routes for controllers are covered and
# do not allow guest access.
#
# There are several other controllers that, for guest test coverage,
# are identical to the Items Controller spec below
# Note: there are several non-CRUD methods; destroy makes a record
# inactive; there is no record removal revealed via the API (client's request)
# spec/controllers/items_controller_spec.rb
require 'spec_helper'
describe ItemsController do
context 'guest' do
describe 'GET to index' do
before { get :index }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'GET to show' do
before { get :show, :id => 1 }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'GET to export' do
before { get :export }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'GET to new' do
before { get :new }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'POST to create' do
before { post :create }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'GET to edit' do
before { get :edit, :id => 1 }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'PUT to update' do
before { put :update, :id => 1 }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'DELETE to destroy' do
before { delete :destroy, :id => 1 }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
describe 'PUT to activate' do
before { put :activate, :id => 1 }
specify { response.should redirect_to(login_path) }
specify { flash.alert.should =~ /You must be logged in to access this page/ }
end
end
end
# spec/controllers/widgets_controller_spec.rb
require 'spec_helper'
describe WidgetsController do
context 'guest' do
# same code as ItemsControllerSpec above
end
end
# spec/controllers/gears_controller_spec.rb
require 'spec_helper'
describe GearsController do
context 'guest' do
# same code as ItemsControllerSpec above
end
end
# etc.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment