Skip to content

Instantly share code, notes, and snippets.

@andrewpthorp
Created June 16, 2011 19:31
Show Gist options
  • Save andrewpthorp/1030036 to your computer and use it in GitHub Desktop.
Save andrewpthorp/1030036 to your computer and use it in GitHub Desktop.
Books Controller Test
class BooksController < ApplicationController
before_filter :authorize, :only => [:new, :create, :edit, :update, :destroy]
load_and_authorize_resource
# GET /books
# GET /books.xml
def index
@title = "Books"
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @books }
end
end
# GET /books/1
# GET /books/1.xml
def show
@title = @book.name
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/new
# GET /books/new.xml
def new
@title = "New Book"
respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @book }
end
end
# GET /books/1/edit
def edit
@title = "Edit Book"
end
# POST /books
# POST /books.xml
def create
respond_to do |format|
if @book.save
format.html { redirect_to(@book, :notice => 'Book was successfully created.') }
format.xml { render :xml => @book, :status => :created, :location => @book }
else
format.html { render :action => "new" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# PUT /books/1
# PUT /books/1.xml
def update
respond_to do |format|
if @book.update_attributes(params[:book])
format.html { redirect_to(@book, :notice => 'Book was successfully updated.') }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @book.errors, :status => :unprocessable_entity }
end
end
end
# DELETE /books/1
# DELETE /books/1.xml
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to(books_url) }
format.xml { head :ok }
end
end
end
require 'spec_helper'
describe BooksController, 'routes' do
it { should route(:get, '/books/new').to(:action => 'new') }
it { should route(:post, '/books').to(:action => 'create') }
it { should route(:get, '/books/1').to(:action => 'show', :id => 1) }
it { should route(:get, '/books/1/edit').to(:action => 'edit', :id => 1) }
it { should route(:put, '/books/1').to(:action => 'update', :id => 1) }
end
describe BooksController, 'authorization' do
it { should require_authentication_for(:get, :new) }
it { should require_authentication_for(:post, :create) }
it { should_not require_authentication_for(:get, :index) }
context 'with a book' do
let(:book) { Factory(:book) }
it { should_not require_authentication_for(:get, :show, :id => book.id) }
it { should require_authentication_for(:get, :edit, :id => book.id) }
it { should require_authentication_for(:put, :update, :id => book.id) }
end
end
@andrewpthorp
Copy link
Author

All other tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment