Skip to content

Instantly share code, notes, and snippets.

@danielvlopes
Created August 22, 2010 14:54
Show Gist options
  • Save danielvlopes/543851 to your computer and use it in GitHub Desktop.
Save danielvlopes/543851 to your computer and use it in GitHub Desktop.
require "spec/acceptance/acceptance_helper"
feature "Login", %q{
In order to access my account
As an user
I want to be able to log in
} do
let :user do
Factory :user
end
let :invalid_user do
mock 'User', :login => "jonhdoe"
end
Scenario "Invalid user" do
log_in_with invalid_user
should_have_error "Fail to login as jonhdoe."
end
scenario "Logout" do
log_in_with user
click "logout"
should_have_notice "Loged out with success."
should_be_on home_page
end
end
module HelperMethods
def log_in_with(user)
visit "/"
fill_in "user", :with => user.login
fill_in "passowrd", :with => "123456"
click "login"
end
[:notice, :error].each do |name|
define_method "should_have_#{name}" do |message|
page.should have_css(".message.#{name}", :text => message)
end
end
def should_be_on(path)
page.current_url.should match(Regexp.new(path))
end
def should_not_be_on(path)
page.current_url.should_not match(Regexp.new(path))
end
end
## HELPER
def should_have_errors(*messages)
within(:css, "#errorExplanation") do
messages.each { |msg| page.should have_content(msg) }
end
end
alias_method :should_have_error, :should_have_errors
## USAGE
scenario "Invalid post" do
click "Create"
should_have_errors(
"body can't be blank",
"category can't be blank",
"publish date can't be blank"
)
end
## HELPER
def fill_the_following(fields={})
fields.each do |field, value|
fill_in field, :with => value
end
end
## USAGE
scenario "Valid Post" do
fill_the_following(
"Post" => "Steak is awesome",
"Publish date" => "31/10/2010",
"Excerpt" => "Bla bla bla ...",
"Body" => "Bla bla bla ..."
)
click "Create"
should_have_notice "Post was successfully created."
end
## HELPER
def should_have_the_following(*contents)
contents.each do |content|
page.should have_content(content)
end
end
## USAGE
scenario "Post with tags" do
post = Factory :post
visit post_path(post)
within(".tags") do
should_have_the_following(
'steak',
'acceptance',
'rails',
'ruby'
)
end
end
## HELPER
def should_have_table(table_name, *rows)
within(table_name) do
rows.each do |columns|
columns.each { |content| page.should have_content(content) }
end
end
end
## USAGE
scenario "List posts" do
visit posts_path
should_have_table "#posts_grid",
[ "Intro to RSpec", "Super awesome post about rspec..." ],
[ "Rails 3.0", "Last week Rails 3.0RC was released..." ]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment