Permalink
Browse files

Generators for RSpec integration suite

+ Feature to check migrations after clearance:install
+ Create generator for intalling rspec specs
+ Updated readme for RSpec integration generator
  • Loading branch information...
1 parent c53c7de commit 7cf9bf776da6cbd886cd2a9414f63c3c52054a76 @harlow harlow committed Oct 15, 2012
Showing with 395 additions and 59 deletions.
  1. +8 −8 README.md
  2. +65 −0 features/add_migrations_to_project.feature
  3. +0 −50 features/integration.feature
  4. +22 −0 features/integration_with_cucumber.feature
  5. +21 −0 features/integration_with_rspec.feature
  6. +21 −0 features/step_definitions/configuration_steps.rb
  7. +1 −1 lib/generators/clearance/features/templates/spec/factories/clearance.rb
  8. +5 −0 lib/generators/clearance/specs/USAGE
  9. +13 −0 lib/generators/clearance/specs/specs_generator.rb
  10. +10 −0 lib/generators/clearance/specs/templates/factories/clearance.rb
  11. +10 −0 lib/generators/clearance/specs/templates/integration/clearance/user_signs_out_spec.rb
  12. +28 −0 lib/generators/clearance/specs/templates/integration/clearance/visitor_resets_password_spec.rb
  13. +42 −0 lib/generators/clearance/specs/templates/integration/clearance/visitor_signs_in_spec.rb
  14. +21 −0 lib/generators/clearance/specs/templates/integration/clearance/visitor_signs_up_spec.rb
  15. +47 −0 lib/generators/clearance/specs/templates/integration/clearance/visitor_updates_password_spec.rb
  16. +3 −0 lib/generators/clearance/specs/templates/support/action_mailer.rb
  17. +1 −0 lib/generators/clearance/specs/templates/support/clearance.rb
  18. +5 −0 lib/generators/clearance/specs/templates/support/factory_girl.rb
  19. +4 −0 lib/generators/clearance/specs/templates/support/integration.rb
  20. +19 −0 lib/generators/clearance/specs/templates/support/integration/action_mailer_helpers.rb
  21. +49 −0 lib/generators/clearance/specs/templates/support/integration/clearance_helpers.rb
View
@@ -280,24 +280,24 @@ You can write a custom password strategy that has two instance methods:
config.password_strategy = CustomPasswordStrategy
end
-Optional Cucumber features
+Optional Integration tests
--------------------------
-Clearance's Cucumber features are dependent on:
+Clearance's integration tests are dependent on:
-* Cucumber
* Capybara
* RSpec
* Factory Girl
-As your app evolves, you want to know that authentication still works. If you've
-installed [Cucumber](http://cukes.info) into your app:
+As your app evolves, you want to know that authentication still works. We have added support for RSpec integration tests.
- rails generate cucumber:install
+If you've installed [RSpec](https://github.com/rspec/rspec) gem in your app:
-Then, you can use the Clearance features generator:
+ rails generate rspec:install
- rails generate clearance:features
+Then, you can use the Clearance specs generator:
+
+ rails generate clearance:specs
Edit your Gemfile to include:
@@ -0,0 +1,65 @@
+Feature: add migrations to the project
+
+ Background:
+ Given I have a project with clearance
+ And I run `bundle install --local`
+
+ Scenario: Users table does not exist
+ And I successfully run `bundle exec rails generate clearance:install`
+ And I successfully run `ls db/migrate`
+ Then the output should contain:
+ """
+ create_users.rb
+ """
+
+ Scenario: Users table without clearance fields exists in the database
+ When I write to "db/migrate/001_create_users.rb" with:
+ """
+ class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table(:users) do |t|
+ t.string :email
+ t.string :name
+ end
+ end
+ def self.down
+ end
+ end
+ """
+ And I successfully run `bundle exec rake db:migrate --trace`
+ And I successfully run `bundle exec rails generate clearance:install`
+ And I successfully run `ls db/migrate`
+ Then the output should contain:
+ """
+ add_clearance_to_users.rb
+ """
+
+ Scenario: Users table with clearance fields exists in the database
+ When I write to "db/migrate/001_create_users.rb" with:
+ """
+ class CreateUsers < ActiveRecord::Migration
+ def self.up
+ create_table :users do |t|
+ t.timestamps :null => false
+ t.string :email, :null => false
+ t.string :encrypted_password, :limit => 128, :null => false
+ t.string :confirmation_token, :limit => 128
+ t.string :remember_token, :limit => 128, :null => false
+ end
+
+ add_index :users, :email
+ add_index :users, :remember_token
+ end
+
+ def self.down
+ drop_table :users
+ end
+ end
+ """
+ And I successfully run `bundle exec rake db:migrate --trace`
+ And I successfully run `bundle exec rails generate clearance:install`
+ And I successfully run `ls db/migrate`
+ Then the output should not contain:
+ """
+ add_clearance_to_users.rb
+ """
@@ -1,50 +0,0 @@
-Feature: integrate with application
-
- Background:
- When I successfully run `bundle exec rails new testapp`
- And I cd to "testapp"
- And I remove the file "public/index.html"
- And I remove the file "app/views/layouts/application.html.erb"
- And I configure ActionMailer to use "localhost" as a host
- And I configure a root route
- And I add the "cucumber-rails" gem
- And I add the "capybara" gem
- And I add the "rspec-rails" gem
- And I add the "factory_girl_rails" gem
- And I add the "database_cleaner" gem
- And I add the "clearance" gem from this project
- And I run `bundle install --local`
- And I successfully run `bundle exec rails generate cucumber:install`
- And I successfully run `bundle exec rails generate clearance:features`
-
- Scenario: generate a Rails app, run the generators, and run the tests
- When I successfully run `bundle exec rails generate clearance:install`
- Then the output should contain "Next steps"
- When I successfully run `bundle exec rake db:migrate --trace`
- And I successfully run `bundle exec rake --trace`
- Then the output should contain "passed"
- And the output should not contain "failed"
- And the output should not contain "Could not find generator"
-
- Scenario: Developer already has a users table in their database
- When I write to "db/migrate/001_create_users.rb" with:
- """
- class CreateUsers < ActiveRecord::Migration
- def self.up
- create_table(:users) do |t|
- t.string :email
- t.string :name
- end
- end
- def self.down
- end
- end
- """
- And I successfully run `bundle exec rake db:migrate --trace`
- And I successfully run `bundle exec rails generate clearance:install`
- And I successfully run `bundle exec rake db:migrate --trace`
- And I successfully run `bundle exec rake --trace`
- Then the output should contain "passed"
- And the output should not contain "failed"
- And the output should not contain "Could not find generator"
-
@@ -0,0 +1,22 @@
+Feature: install and run cucumber features
+
+ Background:
+ Given I have a project with clearance and the following gems:
+ | gem |
+ | cucumber-rails |
+ | capybara |
+ | rspec-rails |
+ | factory_girl_rails |
+ | database_cleaner |
+ And I run `bundle install --local`
+ And I successfully run `bundle exec rails generate cucumber:install`
+ And I successfully run `bundle exec rails generate clearance:features`
+ And I successfully run `bundle exec rails generate clearance:install`
+
+ Scenario: generate a Rails app, run the generators, and run the tests
+ Then the output should contain "Next steps"
+ When I successfully run `bundle exec rake db:migrate --trace`
+ And I successfully run `bundle exec rake --trace`
+ Then the output should contain "passed"
+ And the output should not contain "failed"
+ And the output should not contain "Could not find generator"
@@ -0,0 +1,21 @@
+Feature: generate rspec integration tests with application
+
+ Background:
+ Given I have a project with clearance and the following gems:
+ | gem |
+ | capybara |
+ | rspec-rails |
+ | factory_girl_rails |
+ | database_cleaner |
+ And I run `bundle install --local`
+ And I successfully run `bundle exec rails generate rspec:install`
+ And I successfully run `bundle exec rails generate clearance:specs`
+
+ Scenario: generate a Rails app, run the generators, and run the tests
+ And I successfully run `bundle exec rails generate clearance:install`
+ Then the output should contain "Next steps"
+ When I successfully run `bundle exec rake db:migrate --trace`
+ And I successfully run `bundle exec rspec`
+ Then the output should contain "Finished"
+ And the output should not contain "Failed examples"
+ And the output should not contain "Could not find generator"
@@ -1,3 +1,24 @@
+When "I have a project with clearance and the following gems:" do |table|
+ step "I have a project with clearance"
+
+ table.map_column!('gem') do |gem|
+ step %Q{ And I add the "#{gem}" gem }
+ end
+end
+
+When "I have a project with clearance" do
+ step "I successfully run `bundle exec rails new testapp`"
+
+ steps %Q{
+ And I cd to "testapp"
+ And I remove the file "public/index.html"
+ And I remove the file "app/views/layouts/application.html.erb"
+ And I configure ActionMailer to use "localhost" as a host
+ And I configure a root route
+ And I add the "clearance" gem from this project
+ }
+end
+
When /^I configure ActionMailer to use "([^"]+)" as a host$/ do |host|
mailer_config = "config.action_mailer.default_url_options = { :host => '#{host}' }"
path = 'config/application.rb'
@@ -5,6 +5,6 @@
factory :user do
email
- password "password"
+ password 'password'
end
end
@@ -0,0 +1,5 @@
+Description:
+ Generate RSpec integration tests
+
+Examples:
+ rails generate clearance:specs
@@ -0,0 +1,13 @@
+require 'rails/generators/base'
+
+module Clearance
+ module Generators
+ class SpecsGenerator < Rails::Generators::Base
+ source_root File.expand_path('../templates', __FILE__)
+
+ def create_specs
+ directory '.', 'spec'
+ end
+ end
+ end
+end
@@ -0,0 +1,10 @@
+FactoryGirl.define do
+ sequence :email do |n|
+ "user#{n}@example.com"
+ end
+
+ factory :user do
+ email
+ password 'password'
+ end
+end
@@ -0,0 +1,10 @@
+require 'spec_helper'
+
+feature 'User signs out' do
+ scenario 'signs out' do
+ user = signed_in_user
+ sign_out
+
+ user_should_be_signed_out
+ end
+end
@@ -0,0 +1,28 @@
+require 'spec_helper'
+
+feature 'Visitor resets password' do
+ scenario 'with valid email' do
+ user = user_with_reset_password
+
+ page_should_display_change_password_message
+ reset_notification_should_be_sent_to user
+ end
+
+ scenario 'with non-user account' do
+ reset_password_for 'unknown.email@example.com'
+
+ page_should_display_change_password_message
+ mailer_should_have_no_deliveries
+ end
+
+ private
+
+ def reset_notification_should_be_sent_to(user)
+ user.confirmation_token.should_not be_blank
+ mailer_should_have_delivery user.email, 'password', user.confirmation_token
+ end
+
+ def page_should_display_change_password_message
+ page.should have_content('instructions for changing your password')
+ end
+end
@@ -0,0 +1,42 @@
+require 'spec_helper'
+
+feature 'Visitor signs in' do
+ scenario 'with valid email and password' do
+ create_user 'user@example.com', 'password'
+ sign_in_with 'user@example.com', 'password'
+
+ user_should_be_signed_in
+ end
+
+ scenario 'with valid mixed-case email and password ' do
+ create_user 'user.name@example.com', 'password'
+ sign_in_with 'User.Name@example.com', 'password'
+
+ user_should_be_signed_in
+ end
+
+ scenario 'tries with invalid password' do
+ create_user 'user@example.com', 'password'
+ sign_in_with 'user@example.com', 'wrong_password'
+
+ page_should_display_sign_in_error
+ user_should_be_signed_out
+ end
+
+ scenario 'tries with invalid email' do
+ sign_in_with 'unknown.email@example.com', 'password'
+
+ page_should_display_sign_in_error
+ user_should_be_signed_out
+ end
+
+ private
+
+ def create_user(email, password)
+ create(:user, :email => email, :password => password)
+ end
+
+ def page_should_display_sign_in_error
+ page.should have_content('Bad email or password')
+ end
+ end
@@ -0,0 +1,21 @@
+require 'spec_helper'
+
+feature 'Visitor signs up' do
+ scenario 'with valid email and password' do
+ sign_up_with 'valid@example.com', 'password'
+
+ user_should_be_signed_in
+ end
+
+ scenario 'tries with invalid email' do
+ sign_up_with 'invalid_email', 'password'
+
+ user_should_be_signed_out
+ end
+
+ scenario 'tries with blank password' do
+ sign_up_with 'valid@example.com', ''
+
+ user_should_be_signed_out
+ end
+end
Oops, something went wrong.

0 comments on commit 7cf9bf7

Please sign in to comment.