Skip to content

Instantly share code, notes, and snippets.

@prathe
Created October 8, 2011 22:18
Show Gist options
  • Save prathe/1272973 to your computer and use it in GitHub Desktop.
Save prathe/1272973 to your computer and use it in GitHub Desktop.
Feature: User login
Scenario: User is greeted upon login
Given a user with an account
When he logs in
Then he should be greeted
module UserLoginSteps
def create_user(user = FactoryGirl.build(:user))
user.save_without_session_maintenance
user
end
def login(user)
visit login_path
fill_in 'Email', :with => user.email
fill_in 'Password', :with => user.password
click_on 'Log in'
end
end
World UserLoginSteps
RSpec::Matchers.define :be_greeted do
match do |user|
greeting_message = "Welcome, #{user.name}"
page.has_content? greeting_message
end
failure_message_for_should do
"expected the user to see a greeting message"
end
end
Given /^a user with an account$/ do
@user = create_user
end
When /^he logs in$/ do
login @user
end
Then /^he should be greeted$/ do
@user.should be_greeted
end
Feature: User logout
Scenario: User is kicked off from his account upon logout
Given a logged in user
When he logs out
Then he should be kicked off from his account
module UserLogoutSteps
def logout(user)
visit logout_path
end
end
World UserLogoutSteps
RSpec::Matchers.define :be_kicked_off do
match do |user|
!UserSession.find && current_path != account_path
end
failure_message_for_should do |user|
"expected the user to be kicked off from his account"
end
end
Given /^a logged in user$/ do
@user = create_user
login @user
end
When /^he logs out$/ do
logout @user
end
Then /^he should be kicked off from his account$/ do
@user.should be_kicked_off
end
@prathe
Copy link
Author

prathe commented Oct 9, 2011

Note that these scenarios are declarative and not imperative.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment