Created
March 20, 2010 14:57
-
-
Save phillipkoebbe/338707 to your computer and use it in GitHub Desktop.
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/controllers/admin/messages_controller.rb | |
def stub_user(is_administrator) | |
user = stub_model(User) | |
# because the application is going to call is_administrator in a before filter | |
user.stub(:is_administrator).and_return(is_administrator) | |
# because the application is going to call find_by_id in a before filter | |
User.stub(:find_by_id).and_return(user) | |
user | |
end | |
# helper method to simulate a regular user being logged in | |
def setup_user | |
@user = stub_user(false) | |
session[:user_id] = @user.id | |
end | |
# helper method to simulate an admin being logged in | |
def setup_admin | |
@admin = stub_user(true) | |
session[:user_id] = @admin.id | |
end | |
describe Admin::MessagesController do | |
describe ':delete :destroy' do | |
# this before :each will happen before every example and in every context | |
# in this describe (:delete :destroy) | |
before :each do | |
setup_admin | |
end | |
context 'with valid id' do | |
# this before :each will happen for everything in this context (with valid id) | |
before :each do | |
# we need a fake object for the application to work with | |
@message = stub_model(Message) | |
# set an expectation that the application is supposed to call :find_by_id. | |
# when it does, we need to return the fake object we just created. .with(@message.id.to_s) | |
# is used to make sure the id is being pulled from the params hash. Notice the | |
# delete :destroy line below. It uses the same thing. This is just a safety | |
# precaution in case someone hard-codes a value during experimenation. Unless the | |
# same value is hard-coded (not real likely), this will catch it before it goes | |
# to QA or Production. | |
Message.should_receive(:find_by_id).with(@message.id.to_s).and_return(@message) | |
# setting an expectation that the application code should call object.destroy. Since | |
# this is a destroy action, if this doesn't happen, the action is not doing what it is | |
# supposed to do | |
@message.should_receive(:destroy) | |
# kick it all off! | |
delete :destroy, {:id => @message.id.to_s} | |
end | |
# these macros are from Remarkable, a gem that helps to DRY up your specs. | |
should_not_assign_to :message | |
should_set_the_flash :info, :to => Messages::MESSAGE_DELETED | |
should_redirect_to { admin_messages_path } | |
end | |
context 'with invalid id' do | |
# this before each applies to the examples in this context only | |
before :each do | |
# we want to simulate what happens when an invalid id is provided to the | |
# controller, so return nil. :find_by_id should still be called, but it will | |
# not be successful | |
Message.should_receive(:find_by_id).and_return(nil) | |
# it doesn't really matter what id is used here. In fact, you could probably leave | |
# it out altogether. I prefer not to. | |
get :show, {:id => 1} | |
end | |
should_assign_to :message, :with => nil | |
should_redirect_to { error_path(404) } | |
end | |
end | |
end | |
# app/controllers/admin/messages_controller.rb | |
class Admin::MessagesController < Admin::BaseController | |
def destroy | |
message = Message.find_by_id(params[:id]) | |
redirect_to error_path(404) unless message | |
message.destroy | |
flash[:info] = Messages::MESSAGE_DELETED | |
redirect_to admin_messages_path | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment