Skip to content

Instantly share code, notes, and snippets.

@jaimeiniesta
Created August 25, 2010 08:22
Show Gist options
  • Select an option

  • Save jaimeiniesta/549096 to your computer and use it in GitHub Desktop.

Select an option

Save jaimeiniesta/549096 to your computer and use it in GitHub Desktop.
# spec/acceptance/blog_spec.rb
require File.dirname(__FILE__) + '/acceptance_helper'
feature "Feature name", %q{
In order to have a blog
As an administrator
I want to manage posts
} do
background do
create_sample_posts
create_sample_users
end
context "Authorized as admin" do
before(:each) do
admin_login
end
after(:each) do
admin_logout
end
scenario "List posts" do
visit "/posts"
page.should have_content "The PageRankAlert blog"
page.should have_content "First post"
page.should have_content "Second post"
page.should have_content "Third post"
page.should have_link "New Post"
end
scenario "Create post" do
visit "/posts"
click "New Post"
page.should have_css("h2", "New post")
fill_in "post_title", :with => "Welcome to the blog"
fill_in "post_body", :with => "Yes, you are welcome to the pagerankalert.com blog!!!"
click "Create Post"
page.should have_content "Post was successfully created"
page.should have_content "Welcome to the blog"
page.should have_content "Yes, you are welcome to the pagerankalert.com blog!!!"
page.should have_link "Edit"
end
scenario "Show post" do
visit "/posts/1"
page.should have_content "First post"
page.should have_content "This is the first post"
page.should_not have_content "This is the second post"
page.should have_link "Edit"
end
scenario "Update post" do
visit "/posts"
click "First post"
click "Edit"
page.should have_css("h3", :text => "Editing post")
fill_in "post_title", :with => "The modified title"
fill_in "post_body", :with => "The modified body"
click "Update Post"
page.should have_content("Post was successfully updated.")
page.should have_content("The modified title")
end
scenario "Destroy post" do
visit "/posts"
click "First post"
click "Destroy"
page.should have_content("Post was successfully destroyed.")
page.should have_content("The PageRankAlert blog")
end
end
context "Unauthorized anonymous user" do
scenario "Should list and show posts" do
visit "/posts"
page.should have_content "The PageRankAlert blog"
click "First post"
page.should have_content "This is the first post"
end
scenario "List posts should not show New post link" do
visit "/posts"
page.should_not have_link "New Post"
end
scenario "Show post should not show edit nor destroy links" do
visit "/posts/1"
page.should_not have_link "Edit"
page.should_not have_link "Destroy"
end
scenario "Should not get new post form" do
visit "/posts/new"
page.should_not have_content "New post"
page.should have_content "You need to sign in or sign up before continuing."
end
scenario "Should not get edit post form" do
visit "/posts/1/edit"
page.should_not have_content "Editing post"
page.should have_content "You need to sign in or sign up before continuing."
end
end
context "Unauthorized non-admin user" do
before(:each) do
user_login
end
after(:each) do
user_logout
end
scenario "Should list and show posts" do
visit "/posts"
page.should have_content "The PageRankAlert blog"
click "First post"
page.should have_content "This is the first post"
end
scenario "List posts should not show New post link" do
visit "/posts"
page.should_not have_link "New Post"
end
scenario "Show post should not show edit nor destroy links" do
visit "/posts/1"
page.should_not have_link "Edit"
page.should_not have_link "Destroy"
end
scenario "Should not get new post form" do
visit "/posts/new"
page.should_not have_content "New post"
page.should have_content "You need to sign in or sign up before continuing."
end
scenario "Should not get edit post form" do
visit "/posts/1/edit"
page.should_not have_content "Editing post"
page.should have_content "You need to sign in or sign up before continuing."
end
end
end
###################
# spec/acceptance/support/helpers.rb
module HelperMethods
# Put helper methods you need to be available in all tests here.
# Login as first admin
def admin_login
visit "/admins/sign_in"
fill_in "admin_email", :with => "pagerankalert@gmail.com"
fill_in "admin_password", :with => "admin123"
click "Sign in"
page.should have_content "Signed in successfully."
page.should have_content "Signed in as admin pagerankalert@gmail.com"
end
# Logout as admin
def admin_logout
visit "/admins/sign_out"
page.should have_content "Signed out successfully."
page.should_not have_content "Signed in as admin pagerankalert@gmail.com"
end
# Login as first user
def user_login
visit "/users/sign_in"
fill_in "user_email", :with => "jaimeiniesta@gmail.com"
fill_in "user_password", :with => "test123"
click "Sign in"
page.should have_content "Signed in successfully."
page.should have_content "Signed in as jaimeiniesta@gmail.com"
end
# Logout as user
def user_logout
visit "/users/sign_out"
page.should have_content "Signed out successfully."
page.should_not have_content "Signed in as jaimeiniesta@gmail.com"
end
end
RSpec.configuration.include(HelperMethods)
###################
# spec/acceptance/support/factories.rb
# Creates some posts for the blog
def create_sample_posts
Post.create(:title => "First post", :body => "This is the first post")
Post.create(:title => "Second post", :body => "This is the second post")
Post.create(:title => "Third post", :body => "This is the third post")
end
# Create some sample users and admins
def create_sample_users
Admin.create(:email => "pagerankalert@gmail.com", :password => "admin123", :password_confirmation => "admin123")
User.create(:email => "jaimeiniesta@gmail.com", :password => "test123", :password_confirmation => "test123")
end
##############################
# spec/spech_helper.rb
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.fixture_path = "#{::Rails.root}/spec/fixtures"
# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, comment the following line or assign false
# instead of true.
config.use_transactional_fixtures = true
# Save Capybara temp files for save_and_open_page on temp folder
Capybara.save_and_open_page_path = File.join(Rails.root,'tmp/capybara')
# begin change Capybara default driver (:selenium or :rack-test; default is rack-test)
#require 'capybara/rails'
#Capybara.default_driver = :selenium
# end change Capybara default driver
### begin Database cleaner
require 'database_cleaner'
DatabaseCleaner.strategy = :truncation
config.before(:each) do
DatabaseCleaner.clean
end
### end Database cleaner
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment