Skip to content

Instantly share code, notes, and snippets.

@svs
Created October 5, 2012 06:15
Show Gist options
  • Save svs/3838370 to your computer and use it in GitHub Desktop.
Save svs/3838370 to your computer and use it in GitHub Desktop.
authorised_actions
shared_examples "authorised action" do
before :each do
action.call
end
it "should assign proper items" do
if defined?(variable)
variable.each do |k,v|
assigns[k].should v.call
end
end
end
it "should satisfy expectations" do
if defined?(expectations)
expectations.each do |e|
expect(action).to (e.call)
end
end
end
it "should render proper template/ redirect properly" do
response.should redirect_to(redirect_url) if defined?(redirect_url)
response.should render_template(template) if defined?(template)
end
end
describe ItemsController do
describe "authorised"
describe "other actions" do
before :each do
controller.stubs(:current_user => @user)
Ability.any_instance.stubs(:can?).returns(true)
@item = FactoryGirl.create(:item)
end
describe "new" do
it_should_behave_like "authorised action" do
let(:action) { Proc.new {post :new } }
let(:variables) { {:item => Proc.new{be_a_new(Item)}} }
let(:template) { :new }
end
end
describe "show" do
it_should_behave_like "authorised action" do
let(:action) { Proc.new {post :show, {:id => @item.id} } }
let(:variables) { {:item => lambda{ eq @item} } }
let(:template) { :show }
end
end
describe "edit" do
it_should_behave_like "authorised action" do
let(:action) { Proc.new {get :edit, {:id => @item.id} } }
let(:variables) { {:item => lambda{ eq @item} } }
let(:template) { :edit }
end
end
describe "update" do
before :each do
Item.any_instance.expects(:save).returns(true)
end
it_should_behave_like "authorised action" do
let(:action) { Proc.new {put :update, {:id => @item.id, :item => {}} } }
let(:variables) { {:item => lambda{ eq @item} } }
let(:redirect_url) { @item }
end
end
describe "create" do
it_should_behave_like "authorised action" do
let(:action) { Proc.new {post :create, {:item => {:user_id => 1}} } }
let(:variables) { {:item => lambda{ eq @item} } }
let(:redirect_url) { assigns[:item] }
let(:expectations) { [
lambda{ change(Item, :count).by(1)}
]}
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment