Skip to content

Commit

Permalink
Generators for RSpec integration suite
Browse files Browse the repository at this point in the history
+ Feature to check migrations after clearance:install
+ Create generator for intalling rspec specs
+ Updated readme for RSpec integration generator
  • Loading branch information
harlow committed Oct 16, 2012
1 parent c53c7de commit 7cf9bf7
Show file tree
Hide file tree
Showing 21 changed files with 395 additions and 59 deletions.
16 changes: 8 additions & 8 deletions README.md
Expand Up @@ -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:

Expand Down
65 changes: 65 additions & 0 deletions features/add_migrations_to_project.feature
@@ -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
"""
50 changes: 0 additions & 50 deletions features/integration.feature

This file was deleted.

22 changes: 22 additions & 0 deletions features/integration_with_cucumber.feature
@@ -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"
21 changes: 21 additions & 0 deletions features/integration_with_rspec.feature
@@ -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"
21 changes: 21 additions & 0 deletions features/step_definitions/configuration_steps.rb
@@ -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'
Expand Down
Expand Up @@ -5,6 +5,6 @@

factory :user do
email
password "password"
password 'password'
end
end
5 changes: 5 additions & 0 deletions lib/generators/clearance/specs/USAGE
@@ -0,0 +1,5 @@
Description:
Generate RSpec integration tests

Examples:
rails generate clearance:specs
13 changes: 13 additions & 0 deletions lib/generators/clearance/specs/specs_generator.rb
@@ -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
10 changes: 10 additions & 0 deletions lib/generators/clearance/specs/templates/factories/clearance.rb
@@ -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

0 comments on commit 7cf9bf7

Please sign in to comment.