Skip to content

Instantly share code, notes, and snippets.

@apcomplete
Last active December 29, 2015 03:19
Show Gist options
  • Save apcomplete/7606933 to your computer and use it in GitHub Desktop.
Save apcomplete/7606933 to your computer and use it in GitHub Desktop.
Shared example for request specs
require 'spec_helper'
describe "Scrapbooks" do
context 'beta user' do
before do
@user = FactoryGirl.create :user, :confirmed, roles: [:beta]
@resource = FactoryGirl.create :scrapbook, user: @user
@other_resource = FactoryGirl.create :scrapbook
end
include_examples 'accessible when authenticated', Scrapbook
end
end
shared_examples "accessible when authenticated" do |klass|
let!(:described_model) { klass.model_name }
let!(:controller) { klass.model_name.route_key }
describe "GET /#{klass.model_name.route_key}" do
before do
get url_for(controller: controller, action: :index)
end
context 'when logged in', :role => :user do
it "works!" do
response.status.should be(200)
end
end
context 'when not logged in' do
it_should_deny_access
end
end
describe "GET /#{klass.model_name.route_key}/:id" do
before do
get url_for(controller: controller, action: :show, id: @resource.id)
end
context 'when logged in', :role => :user do
it "works!" do
response.status.should be(200)
end
end
context 'when not logged in' do
it_should_deny_access
end
end
describe "POST /#{klass.model_name.route_key}" do
before do
model_symbol = described_model.singular.to_sym
post url_for(controller: controller, action: :create, params: { model_symbol => FactoryGirl.build(model_symbol) })
end
context 'when logged in', role: :user do
it "works!" do
response.status.should be(200)
end
end
context 'when not logged in' do
it_should_deny_access
end
end
describe "PUT /#{klass.model_name.route_key}/:id" do
before do
model_symbol = described_model.singular.to_sym
put url_for(controller: controller, action: :update, id: @resource.id, params: { model_symbol => @resource.attributes })
end
context 'when logged in', role: :user do
it "works!" do
response.status.should be(200)
end
end
context 'when not logged in' do
it_should_deny_access
end
end
describe "DELETE /#{klass.model_name.route_key}/:id" do
before do
model_symbol = described_model.singular.to_sym
put url_for(controller: controller, action: :destroy, id: @resource.id)
end
context 'when logged in', role: :user do
it "works!" do
response.status.should be(200)
end
end
context 'when not logged in' do
it_should_deny_access
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment