Created
April 28, 2011 13:49
-
-
Save choxi/946380 to your computer and use it in GitHub Desktop.
Example of a Rails Controller Spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
###################### SPEC ###################### | |
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') | |
describe JobsController, "#new" do | |
context "admin" do | |
before(:each) do | |
session[:password] = ADMIN_PASSWORD | |
get :new | |
end | |
it "should set @job" do | |
assigns[:job].should_not be_nil | |
end | |
it "should assign a new job which is a Job" do | |
assigns[:job].should be_instance_of(Job) | |
end | |
end | |
context "not admin" do | |
it "should redirect to the root page" do | |
get :new | |
response.should redirect_to root_path | |
end | |
end | |
end | |
describe JobsController, "#create" do | |
before(:each) do | |
@job = mock(:job, :save => true) | |
Job.stub!(:new).and_return(@job) | |
@job_params = {"title" => "some title", "description" => "some description", | |
"application_process" => "some process", "contact_email" => "bob@inventables.com", | |
"company" => {"name" => "Inventables", "url" => "http://inventables.com"}, | |
"location" => {"city" => "Chicago", "state" => "Illinois"}} | |
end | |
def do_post(params = {}) | |
post :create, {:job => @job_params}.merge(params) | |
end | |
context "admin" do | |
before(:each) do | |
session[:password] = ADMIN_PASSWORD | |
end | |
it "should call create with the parameters" do | |
Job.should_receive(:new).with(@job_params) | |
do_post | |
end | |
it "should save the new job" do | |
@job.should_receive(:save) | |
do_post | |
end | |
it "should set flash success" do | |
do_post | |
flash[:success].should_not be_nil | |
end | |
it "should redirect to the job page" do | |
do_post | |
response.should be_redirect | |
response.should redirect_to(job_path(@job)) | |
end | |
end | |
context "not admin" do | |
it "should redirect to the home page" do | |
do_post | |
response.should redirect_to(root_path) | |
end | |
end | |
end | |
describe JobsController, "#show" do | |
before(:each) do | |
@job = mock(:job) | |
Job.stub!(:find).and_return @job | |
end | |
it "should be a success" do | |
get :show, :id => "123abc" | |
response.should be_success | |
end | |
it "should set @job with the job" do | |
Job.should_receive(:find).with("abc123").and_return(@job) | |
get :show, :id => "abc123" | |
assigns[:job].should == @job | |
end | |
end | |
describe JobsController, "#index" do | |
it "should set @jobs with the jobs" do | |
@jobs = mock(:jobs) | |
Job.should_receive(:all).and_return(@jobs) | |
get :index | |
assigns[:jobs].should == @jobs | |
end | |
end | |
describe JobsController, "#edit" do | |
def do_get | |
get :edit, :id => "some id" | |
end | |
context "not admin" do | |
it "should redirect to the root url" do | |
Job.stub(:find) | |
do_get | |
response.should redirect_to(root_url) | |
end | |
end | |
context "admin" do | |
before(:each) do | |
session[:password] = ADMIN_PASSWORD | |
end | |
it "should call find with the job_id" do | |
Job.should_receive(:find).with("some id") | |
do_get | |
end | |
it "should set @job with the job" do | |
job = mock(:job) | |
Job.stub(:find).and_return(job) | |
do_get | |
assigns[:job].should == job | |
end | |
end | |
end | |
describe JobsController, "#update" do | |
def do_put(params = {}) | |
put :update, { :id => "some job id" }.merge(params) | |
end | |
context "not admin" do | |
it "should redirect to the root url" do | |
do_put | |
response.should redirect_to(root_url) | |
end | |
end | |
context "admin" do | |
before(:each) do | |
session[:password] = ADMIN_PASSWORD | |
@job = mock(:job) | |
Job.stub(:find).and_return(@job) | |
@job.stub(:update_attributes).and_return(true) | |
end | |
it "should call find with the job id" do | |
Job.should_receive(:find).with("some job id") | |
do_put | |
end | |
it "should call update the attributes on the job" do | |
job_params = {"something" => "some params"} | |
@job.should_receive(:update_attributes).with(job_params) | |
do_put :job => job_params | |
end | |
it "should redirect to the job on a succesful update" do | |
do_put | |
response.should redirect_to(job_path(@job)) | |
end | |
it "should redirect back to the form on an unsuccesful update" do | |
@job.stub(:update_attributes).and_return(false) | |
do_put | |
response.should redirect_to(edit_job_path(@job)) | |
end | |
end | |
end | |
describe JobsController, "#destroy" do | |
def do_destroy | |
delete :destroy, :id => "some job id" | |
end | |
context "admin" do | |
before(:each) do | |
session[:password] = ADMIN_PASSWORD | |
@job = mock(:job).as_null_object | |
Job.should_receive(:find).with("some job id").and_return(@job) | |
end | |
it "should find the job" do | |
do_destroy | |
end | |
it "should call destroy on the job" do | |
@job.should_receive(:destroy) | |
do_destroy | |
end | |
it "should set flash success on destroy" do | |
do_destroy | |
flash[:success].should_not be_nil | |
end | |
it "should redirect to the index page" do | |
do_destroy | |
response.should redirect_to(root_path) | |
end | |
end | |
context "not admin" do | |
it "should not find the job" do | |
Job.should_not_receive(:find) | |
do_destroy | |
end | |
it "should redirect to the index page" do | |
do_destroy | |
response.should redirect_to(root_path) | |
end | |
end | |
end | |
###################### CONTROLLER ###################### | |
class JobsController < ApplicationController | |
before_filter :admin_only, :except => ['index', 'show'] | |
def destroy | |
job = Job.find(params[:id]) | |
job.destroy | |
flash[:success] = "Removed job: #{job.title} at #{job.company.name}" | |
redirect_to root_path | |
end | |
def new | |
@job = Job.new | |
@job.build_location | |
end | |
def create | |
@job = Job.new(params[:job]) | |
if @job.save | |
flash[:success] = "Ad Posted!" | |
redirect_to job_path(@job) | |
else | |
render :action => "new" | |
end | |
end | |
def show | |
@job = Job.find(params[:id]) | |
render_404 and return unless @job | |
end | |
def index | |
@jobs = Job.all.reverse | |
end | |
def edit | |
@job = Job.find(params[:id]) | |
end | |
def update | |
@job = Job.find(params[:id]) | |
if @job.update_attributes(params[:job]) | |
redirect_to job_path(@job) | |
else | |
redirect_to edit_job_path(@job) | |
end | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment