Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Forked from justinko/gist:880921
Created March 22, 2011 12:00
Show Gist options
  • Save dchelimsky/881111 to your computer and use it in GitHub Desktop.
Save dchelimsky/881111 to your computer and use it in GitHub Desktop.
# -------------------------------------
# DECLARATION
# using strings for explicit inclusion
shared 'logged in as admin' do
before { login_as admin_user }
end
shared 'a model with a slug' do
it 'sets the slug column upon initialization' do
described_class.new.slug.should_not be_blank
end
end
# adding metadata for implicit inclusion
shared 'with admin user defined', :type => :controller do
let(:admin_user) { Factory(:user, :role => 'admin') }
end
# possible aliases
alias_method :shared_context, :shared # instead of for_groups_matching
alias_method :shared_examples, :shared
# backward compatibility
alias_method :shared_examples_for, :shared
# -------------------------------------
# INCLUSION
# in spec/controllers/widgets_controller_spec.rb
# ^^ implicitly adds :type => :controller
describe WidgetsController do
before do
# admin_user is made available implicitly due to :type => :controller
login_as admin_user
end
# ...
end
# .. or ..
describe WidgetsController do
describe "GET index" do
# explicit inclusion in current example group
include_context "logged in as admin"
# ...
end
end
# in spec/models/post_spec.rb
describe Post do
# generates a nested group
it_behaves_like 'a model with a slug'
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment