Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Ensure features that execute bundle run properly

Bundle commands in cucumber features were using the parent bundler
environment causing the tests to simulate installation improperly.
Thus, wrapping the scenarios in `Bundler.with_clean_env` block ensures
a clean bundler environment for tests.

Note: each scenario must install its own bundle to ensure a clean
environment.

In this commit:
* Improve cucumber feature tests
* Refactor tests for readability
* Rename integration spec directory to features
  • Loading branch information...
commit c408553da9d1dcfe9e4bf38f456545ad3a47b7e9 1 parent be7984c
@gylaz gylaz authored
View
44 features/add_migrations_to_project.feature
@@ -2,9 +2,9 @@ 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
+ When I successfully run `bundle install`
And I successfully run `bundle exec rails generate clearance:install`
And I successfully run `ls db/migrate`
Then the output should contain:
@@ -13,20 +13,9 @@ Feature: add migrations to the project
"""
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`
+ When I successfully run `bundle install`
+ And I create a simple migration
+ And I successfully run `bundle exec rake db:migrate`
And I successfully run `bundle exec rails generate clearance:install`
And I successfully run `ls db/migrate`
Then the output should contain:
@@ -35,28 +24,9 @@ Feature: add migrations to the project
"""
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`
+ When I successfully run `bundle install`
+ And I create a migration with clearance fields
+ And I successfully run `bundle exec rake db:migrate`
And I successfully run `bundle exec rails generate clearance:install`
And I successfully run `ls db/migrate`
Then the output should not contain:
View
9 features/integration_with_rspec.feature
@@ -7,14 +7,15 @@ Feature: generate rspec integration tests with application
| 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
+ When I successfully run `bundle install`
+ And I successfully run `bundle exec rails generate rspec:install`
+ And I successfully run `bundle exec rails generate clearance:specs`
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`
+ When I successfully run `bundle exec rake db:migrate`
+ And I successfully run `bundle exec rake db:test:prepare`
And I successfully run `bundle exec rspec`
Then the output should contain "Finished"
And the output should not contain "Failed examples"
View
49 features/integration_with_test_unit.feature
@@ -1,43 +1,16 @@
-Feature: integrate with test unit
+Feature: integrate with test-unit
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 "factory_girl_rails" gem
- And I run `bundle install --local`
+ Given I have a project with clearance and the following gems:
+ | gem |
+ | factory_girl_rails |
+ | cucumber-rails |
Scenario: generate a Rails app, run the generators, and run the tests
- When I successfully run `bundle exec rails generate clearance:install`
- And I successfully run `bundle exec rake db:migrate --trace`
+ When I successfully run `bundle install`
+ And I successfully run `bundle exec rails generate clearance:install`
+ And I successfully run `bundle exec rake db:migrate`
And I successfully run `bundle exec rails generate controller posts index`
- And I add the "cucumber-rails" gem
- And I write to "test/test_helper.rb" with:
- """
- ENV['RAILS_ENV'] = 'test'
- require File.expand_path('../../config/environment', __FILE__)
- require 'rails/test_help'
-
- class ActiveSupport::TestCase
- fixtures :all
- end
-
- require 'clearance/testing'
- """
- And I write to "test/functionals/posts_controller_test.rb" with:
- """
- require 'test_helper'
-
- class PostsControllerTest < ActionController::TestCase
- test 'should get index' do
- sign_in
- get :index
- assert_response :success
- end
- end
- """
- And I successfully run `bundle exec rake --trace`
- Then the output should contain "1 tests, 1 assertions, 0 failures"
+ And I configure test-unit
+ And I successfully run `bundle exec rake`
+ Then the output should match /1 tests.+1 assertions/
View
93 features/step_definitions/configuration_steps.rb
@@ -1,16 +1,18 @@
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 }
+ table.rows.flatten.each do |gem|
+ step %Q{I add the "#{gem}" gem}
end
end
When "I have a project with clearance" do
- step "I successfully run `bundle exec rails new testapp`"
+ Bundler.with_original_env do
+ step "I successfully run `bundle exec rails new testapp --skip-bundle`"
+ end
steps %Q{
- And I cd to "testapp"
+ When 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
@@ -49,8 +51,83 @@ def show
CONTROLLER
end
-When /^I copy the locked Gemfile from this project$/ do
- in_current_dir do
- FileUtils.cp(File.join(PROJECT_ROOT, 'Gemfile.lock'), 'Gemfile.lock')
- end
+When /^I configure test-unit$/ do
+ factories_path = File.join(
+ 'lib', 'generators', 'clearance', 'specs', 'templates', 'factories',
+ 'clearance.rb'
+ )
+ steps %Q{
+ And I write to "test/test_helper.rb" with:
+ """
+ ENV['RAILS_ENV'] = 'test'
+ require File.expand_path('../../config/environment', __FILE__)
+ require 'rails/test_help'
+
+ class ActiveSupport::TestCase
+ fixtures :all
+ end
+
+ require 'clearance/testing'
+ """
+ And I write to "test/functional/posts_controller_test.rb" with:
+ """
+ require 'test_helper'
+
+ class PostsControllerTest < ActionController::TestCase
+ test 'should get index' do
+ sign_in
+ get :index
+ assert_response :success
+ end
+ end
+ """
+ And I write to "test/factories.rb" with:
+ """
+ #{File.read(factories_path)}
+ """
+ }
+end
+
+When /^I create a simple migration$/ do
+ steps %Q{
+ 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
+ """
+ }
+end
+
+When /^I create a migration with clearance fields$/ do
+ steps %Q{
+ 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
+ """
+ }
end
View
6 features/support/env.rb
@@ -26,3 +26,9 @@
rescue NameError
raise 'You need to add database_cleaner to your Gemfile (in the :test group) if you wish to use it.'
end
+
+Around do |scenario, block|
+ Bundler.with_clean_env do
+ block.call
+ end
+end
View
0  ...ates/integration/clearance/user_signs_out_spec.rb → ...mplates/features/clearance/user_signs_out_spec.rb
File renamed without changes
View
0  ...gration/clearance/visitor_resets_password_spec.rb → ...eatures/clearance/visitor_resets_password_spec.rb
File renamed without changes
View
0  ...es/integration/clearance/visitor_signs_in_spec.rb → ...lates/features/clearance/visitor_signs_in_spec.rb
File renamed without changes
View
0  ...es/integration/clearance/visitor_signs_up_spec.rb → ...lates/features/clearance/visitor_signs_up_spec.rb
File renamed without changes
View
0  ...ration/clearance/visitor_updates_password_spec.rb → ...atures/clearance/visitor_updates_password_spec.rb
File renamed without changes
View
6 lib/generators/clearance/specs/templates/support/features.rb
@@ -0,0 +1,6 @@
+Dir[Rails.root.join('spec/support/features/*.rb')].each { |f| require f }
+
+RSpec.configure do |config|
+ config.include Features::ClearanceHelpers, :type => :feature
+ config.include Features::ActionMailerHelpers, :type => :feature
+end
View
2  ...ates/support/integration/action_mailer_helpers.rb → ...mplates/support/features/action_mailer_helpers.rb
@@ -1,4 +1,4 @@
-module Integration
+module Features
module ActionMailerHelpers
def mailer_should_have_delivery(recipient, subject, body)
ActionMailer::Base.deliveries.should_not be_empty
View
2  ...emplates/support/integration/clearance_helpers.rb → ...s/templates/support/features/clearance_helpers.rb
@@ -1,4 +1,4 @@
-module Integration
+module Features
module ClearanceHelpers
def sign_up_with(email, password)
visit sign_up_path
View
6 lib/generators/clearance/specs/templates/support/integration.rb
@@ -1,6 +0,0 @@
-Dir[Rails.root.join("spec/support/integration/*.rb")].each { |f| require f }
-
-RSpec.configure do |config|
- config.include Integration::ClearanceHelpers, :type => :request
- config.include Integration::ActionMailerHelpers, :type => :request
-end
Please sign in to comment.
Something went wrong with that request. Please try again.