Skip to content

Instantly share code, notes, and snippets.

@rianrainey
Created June 12, 2013 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rianrainey/5765627 to your computer and use it in GitHub Desktop.
Save rianrainey/5765627 to your computer and use it in GitHub Desktop.
Rspec Shared Examples with Rails URL Helper
shared_examples_for "an admin index page" do
it "has an admin navigation bar" do
page.has_selector?("nav.navbar").should be_true
end
it "has a User Management link in the navigation bar" do
page.has_content?("User Management").should be_true
end
it "has an h1 title tag" do
page.has_selector?("h1").should be_true
end
it "has a styled table" do
page.has_selector?("table.table-bordered.table-striped").should be_true
end
it "has an Actions column" do
page.has_selector?("th", text: "Actions").should be_true
end
end
RSpec.configure do |config|
# ...
# Include url_helpers for Shared Examples
config.include Rails.application.routes.url_helpers
# ...
end
shared_examples_for "allow only super admins" do | options |
it "redirects to login for anonymous user" do
visit send(options[:target_path])
current_path.should == send(options[:redirect_path])
end
it "redirects to login for facilitators" do
options[:controller].any_instance.stub(:current_user).and_return(facilitator)
visit send(options[:target_path])
current_path.should == send(options[:redirect_path])
end
it "redirects to login for business administrators" do
options[:controller].any_instance.stub(:current_user).and_return(BusinessAdministrator.first)
visit send(options[:target_path])
current_path.should == send(options[:redirect_path])
end
it "allows super admins to target_path" do
options[:controller].any_instance.stub(:current_user).and_return(SuperAdministrator.first)
visit send(options[:target_path])
current_path.should == send(options[:target_path])
end
end
require 'spec_helper'
describe "Organizations Index View" do
it_should_behave_like "allow only super admins", { :controller => Admin::OrganizationsController,
:target_path => :admin_organizations_path,
:redirect_path => :new_user_session_path }
context "when user is a super admin" do
it_should_behave_like "an admin index page"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment