Skip to content

Instantly share code, notes, and snippets.

@qrush
Created September 17, 2008 15:11
Show Gist options
  • Save qrush/11244 to your computer and use it in GitHub Desktop.
Save qrush/11244 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../spec_helper'
def check_redirects
[:new, :create, :edit, :update, :delete].each do |action|
it "#{action} should redirect" do
get action
response.should be_redirect
end
end
end
def check_users
it "should allow anonymous users to view" do
end
it "should allow logged-in users to view" do
login_as :quentin
end
it "should allow admin users to view" do
login_as :admin
end
end
describe PostsController do
fixtures :all
describe "when post list is accessed" do
check_users
after do
get :index
response.should render_template(:index)
end
end
describe "when a specific post is accessed" do
check_users
after do
get :show, :id => Post.first
response.should render_template(:show)
end
end
describe "when anon users access admin pages" do
check_redirects
end
describe "when normal users access admin pages" do
before do
login_as :quentin
end
check_redirects
end
describe "when admin users are logged in" do
before do
login_as :admin
end
it "new action should render new template" do
get :new
response.should render_template(:new)
end
it "create action should render new template when model is invalid" do
Post.any_instance.stubs(:valid?).returns(false)
post :create
response.should render_template(:new)
end
it "create action should redirect when model is valid" do
Post.any_instance.stubs(:valid?).returns(true)
post :create, {:post => {:title => 'More Cars!', :content => 'They are everywhere!'}}
response.should redirect_to(post_url(assigns[:post]))
end
it "edit action should render edit template" do
get :edit, :id => Post.first
response.should render_template(:edit)
end
it "update action should render edit template when model is invalid" do
Post.any_instance.stubs(:valid?).returns(false)
put :update, :id => Post.first
response.should render_template(:edit)
end
it "update action should redirect when model is valid" do
Post.any_instance.stubs(:valid?).returns(true)
put :update, :id => Post.first
response.should redirect_to(post_url(assigns[:post]))
end
it "destroy action should destroy model and redirect to index action" do
post = Post.first
delete :destroy, :id => post
response.should redirect_to(posts_url)
Post.exists?(post.id).should be_false
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment