Skip to content

Commit

Permalink
Make RSpec stories work.
Browse files Browse the repository at this point in the history
Add spec:acceptance rake task.
Make stories/all.rb run correctly.
Get WebRAT to work with RSpec stories.
Add some more login story scenarios.
  • Loading branch information
booch committed Jul 19, 2008
1 parent c0ac4bc commit b2c7e48
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 8 deletions.
9 changes: 9 additions & 0 deletions lib/tasks/stories.rake
@@ -0,0 +1,9 @@
require 'rake'

namespace :spec do
desc 'Run the stories / acceptance tests'
task :acceptance do
sh 'ruby stories/all.rb --format plain'
end
end

4 changes: 2 additions & 2 deletions stories/all.rb
@@ -1,4 +1,4 @@
dir = File.dirname(__FILE__)
Dir[File.expand_path("#{dir}/**/*.rb")].uniq.each do |file|
require file
end
require file unless file.match(/_helper|_steps/)
end
51 changes: 48 additions & 3 deletions stories/steps/login_steps.rb
@@ -1,3 +1,19 @@
module InstanceVariableHelpers
def set_ivar(type, name, obj)
instance_variable_set ivar_name(type, name), obj
end
def get_ivar(type, name)
returning instance_variable_get(ivar_name(type, name)) do |obj|
yield obj if block_given?
end
end
private
def ivar_name(type, name)
"@#{type}_#{name.gsub(/[ -]/,'_').gsub('&','and')}"
end
end
include InstanceVariableHelpers

module Spec
module Rails
module Matchers
Expand Down Expand Up @@ -33,6 +49,16 @@ def be_an(klass)

steps_for(:login) do

Given "I am logged in as '$username'" do |username|
@user = User.find(:first, :conditions => {:login => username}) ||
User.create!(:login => username, :email => 'test@example.com',
:password => 'password', :password_confirmation => 'password')
visits '/session/login'
fills_in :login, :with => username
fills_in :password, :with => 'password'
clicks_button
end

Given "the '$username' user exists" do |username|
@user = User.find(:first, :conditions => {:login => username}) ||
User.create!(:login => username, :email => 'test@example.com',
Expand All @@ -53,8 +79,9 @@ def be_an(klass)
@user.save!
end

Then "the flash should say '$text'" do |text|
response.should have_text(/#{text}/i)
Given "has $number failed login attempts" do |number|
@user.failed_attempts = number
@user.save!
end

Given "has a password of '$password'" do |password|
Expand All @@ -63,9 +90,13 @@ def be_an(klass)
@user.save!
end

Then "the flash should say '$text'" do |text|
response.should have_text(/#{text}/i)
end

Then "I should be authenticated" do
session[:user_id].should_not be_nil
session[:user_id].should be_an(Integer) # NOTE: Might actually be a String, due to the way sessions are handled.
session[:user_id].should be_an(Integer)
user = User.find(session[:user_id])
user.login.should == @user.login
end
Expand All @@ -74,4 +105,18 @@ def be_an(klass)
session[:user_id].should be_nil
end

Then "I should be logged out" do
session[:user_id].should be_nil
end

Then "my account should have a recent last_login_at timestamp" do
user = User.find(session[:user_id])
(Time.now - user.last_login_at).should < 2 # Allow 2 seconds
end

Then "the '$username' account should be locked" do |username|
User.find_by_login(username).should be_suspended
end


end
4 changes: 1 addition & 3 deletions stories/steps/webrat_steps.rb
@@ -1,9 +1,7 @@
steps_for(:webrat) do




When "(I )?click on the '$link'" do |link|
When "I click on the '$link' link" do |link|
clicks_link link
end

Expand Down
16 changes: 16 additions & 0 deletions stories/stories/login.txt
Expand Up @@ -6,6 +6,7 @@ So that I can use all the features of the site.


Scenario: Valid username/password

Given I am logged out
And the 'test' user exists
And has a password of 'test'
Expand All @@ -17,8 +18,11 @@ When I visit '/session/login'
Then I should be authenticated
And I should end up at the home page
And the flash should say 'logged in'
And my account should have a recent last_login_at timestamp


Scenario: Invalid username

Given I am logged out
And the 'invalid username' user does not exist
When I visit '/session/login'
Expand All @@ -29,7 +33,9 @@ Then I should NOT be authenticated
And I should end up at the login page
And the flash should say 'incorrect username or password'


Scenario: Invalid password

Given I am logged out
And the 'test' user exists
And has a password of 'test'
Expand All @@ -44,6 +50,7 @@ Then I should NOT be authenticated


Scenario: Account has not been activated

Given I am logged out
And the 'test' user exists
And has a password of 'test'
Expand All @@ -56,3 +63,12 @@ Then I should NOT be authenticated
And I should end up at the login page
And the flash should say 'account has not been activated'


Scenario: Logging out

Given I am logged in as 'client'
When I visit '/session/logout'
Then I should be logged out
And I should end up at the login page
And the flash should say 'logged out'

File renamed without changes.

0 comments on commit b2c7e48

Please sign in to comment.