Created
November 13, 2012 15:24
-
-
Save universal/4066308 to your computer and use it in GitHub Desktop.
controller spec example with cancan
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
class AttendancesController < ApplicationController | |
before_filter :authenticate_user! | |
load_and_authorize_resource | |
def show | |
end | |
def update | |
@attendance.update_attributes params[:attendance] | |
redirect_to root_path | |
end | |
def create | |
@attendance.save | |
redirect_to :back | |
end | |
end |
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
require 'spec_helper' | |
describe AttendancesController do | |
context "logged in" do | |
let(:user) {FactoryGirl.create(:user)} | |
before (:each) do | |
sign_in user | |
end | |
context "given an attendance" do | |
let(:attendance) {FactoryGirl.create(:attendance, :user => user)} | |
describe "GET 'show'" do | |
it "authorizes access" do | |
ability = Object.new | |
ability.extend(CanCan::Ability) | |
controller.stub!(:current_ability).and_return(ability) | |
ability.should_receive(:can?) | |
get :show, :id => attendance.id | |
end | |
context "shared" do | |
before(:each) do | |
get :show, :id => attendance.id | |
end | |
it {should respond_with(:success)} | |
it {should render_template(:show)} | |
it {should assign_to(:attendance).with(attendance)} | |
end # shared | |
end # GET 'show' | |
describe "PUT 'update'" do | |
before(:each) do | |
request.env["HTTP_REFERER"] = root_path | |
end | |
it "authorizes access" do | |
ability = Object.new | |
ability.extend(CanCan::Ability) | |
controller.stub!(:current_ability).and_return(ability) | |
ability.should_receive(:can?) | |
put :update, :id => attendance.id, :attendance => {} | |
end | |
context "with valid params" do | |
before(:each) do | |
Attendance.stub(:find).and_return(attendance) | |
attendance.stub(:valid?).and_return(true) | |
end | |
it "updates attedance" do | |
attendance.should_receive(:update_attributes).and_return(true) | |
put :update, :id => attendance.id, :attendance => {}, :format => :js | |
end | |
end | |
end # PUT 'update' | |
end # attendance | |
context "POST 'create'" do | |
it "authorizes access" do | |
ability = Object.new | |
ability.extend(CanCan::Ability) | |
controller.stub!(:current_ability).and_return(ability) | |
ability.should_receive(:can?) | |
post :create, :attendance => {} | |
end | |
end | |
end # logged in | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment