1
0
Fork 0
mirror of https://github.com/thoughtbot/shoulda-matchers.git synced 2022-11-09 12:01:38 -05:00

Refactor spec_helper

The code that sets up the blank Rails application we use for testing is
a little messy. Let's use an object to encapsulate this and then refer
to this object every time we access Rails.application.
This commit is contained in:
Elliot Winkler 2013-11-22 20:25:27 -07:00
parent 4440445baf
commit 8a0bf45d0f
3 changed files with 109 additions and 48 deletions

View file

@ -1,52 +1,22 @@
# Create Rails environment based on the version given from Appraisal
TESTAPP_ROOT = File.join(File.dirname(__FILE__), '..', 'tmp', 'aruba', 'testapp')
require File.expand_path('../support/test_application', __FILE__)
FileUtils.rm_rf(TESTAPP_ROOT) if File.exists?(TESTAPP_ROOT)
$test_app = TestApplication.new
$test_app.create
$test_app.load
`rails new #{TESTAPP_ROOT} --skip-bundle`
ENV['RUBYOPT'] = ""
Dir.chdir(TESTAPP_ROOT) do
retry_count = 0
loop do
puts "Current directory: #{Dir.pwd}"
%w(RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE).each do |key|
puts "#{key}: #{ENV[key].inspect}"
end
command = 'bundle install'
output = Bundler.with_clean_env { `#{command} 2>&1` }
if $? == 0
break
else
retry_count += 1
if retry_count == 3
raise "Command '#{command}' failed:\n#{output}"
end
end
end
end
ENV['BUNDLE_GEMFILE'] ||= TESTAPP_ROOT + '/Gemfile'
ENV['BUNDLE_GEMFILE'] ||= app.gemfile_path
ENV['RAILS_ENV'] = 'test'
require "#{TESTAPP_ROOT}/config/environment"
require 'bourne'
require 'shoulda-matchers'
require 'rspec/rails'
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..')).freeze
PROJECT_ROOT = File.expand_path('../..', __FILE__)
$LOAD_PATH << File.join(PROJECT_ROOT, 'lib')
Dir[File.join(PROJECT_ROOT, 'spec', 'support', '**', '*.rb')].each { |file| require(file) }
# Run the migrations
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate(Rails.root.join('db/migrate'))
Dir[ File.join(PROJECT_ROOT, 'spec/support/**/*.rb') ].each { |file| require file }
RSpec.configure do |config|
config.mock_with :mocha
config.include Shoulda::Matchers::ActionController,
:example_group => { :file_path => /action_controller/ }
example_group: { file_path: /action_controller/ }
end

View file

@ -1,7 +1,4 @@
module ControllerBuilder
TMP_VIEW_PATH = File.expand_path(File.join(TESTAPP_ROOT, 'tmp',
'views')).freeze
def self.included(example_group)
example_group.class_eval do
after do
@ -18,8 +15,7 @@ module ControllerBuilder
end
def define_routes(&block)
Rails.application.routes.draw(&block)
@routes = Rails.application.routes
@routes = $test_app.draw_routes(&block)
class << self
include ActionDispatch::Assertions
end
@ -33,7 +29,7 @@ module ControllerBuilder
layout false
define_method(action, &block)
end
controller_class.view_paths = [TMP_VIEW_PATH]
controller_class.view_paths = [ $test_app.temp_views_dir_path ]
define_routes do
get 'examples', :to => "examples##{action}"
@ -59,15 +55,13 @@ module ControllerBuilder
end
def create_view(path, contents)
full_path = File.join(TMP_VIEW_PATH, path)
FileUtils.mkdir_p(File.dirname(full_path))
File.open(full_path, 'w') { |file| file.write(contents) }
$test_app.create_temp_view(path, contents)
end
private
def delete_temporary_views
FileUtils.rm_rf(TMP_VIEW_PATH)
$test_app.delete_temp_views
end
def restore_original_routes

View file

@ -0,0 +1,97 @@
require 'fileutils'
class TestApplication
ROOT_DIR = File.expand_path('../../../tmp/aruba/testapp', __FILE__)
def create
clean
generate
within_app { install_gems }
end
def load
load_environment
run_migrations
end
def gemfile_path
File.join(ROOT_DIR, 'Gemfile')
end
def environment_file_path
File.join(ROOT_DIR, 'config/environment')
end
def temp_views_dir_path
File.join(ROOT_DIR, 'tmp/views')
end
def create_temp_view(path, contents)
full_path = temp_view_path_for(path)
FileUtils.mkdir_p(File.dirname(full_path))
File.open(full_path, 'w') { |file| file.write(contents) }
end
def delete_temp_views
FileUtils.rm_rf(temp_views_dir_path)
end
def draw_routes(&block)
Rails.application.routes.draw(&block)
Rails.application.routes
end
private
def migrations_dir_path
File.join(ROOT_DIR, 'db/migrate')
end
def temp_view_path_for(path)
File.join(temp_views_dir_path, path)
end
def clean
FileUtils.rm_rf(ROOT_DIR)
end
def generate
`rails new #{ROOT_DIR} --skip-bundle`
end
def load_environment
require environment_file_path
end
def run_migrations
ActiveRecord::Migration.verbose = false
ActiveRecord::Migrator.migrate(migrations_dir_path)
end
def install_gems
retrying('bundle install') do |command|
Bundler.with_clean_env { `#{command}` }
end
end
def within_app(&block)
Dir.chdir(ROOT_DIR, &block)
end
def retrying(command, &runner)
runner ||= -> { `#{command}` }
retry_count = 0
loop do
output = runner.call("#{command} 2>&1")
if $? == 0
break
else
retry_count += 1
if retry_count == 3
raise "Command '#{command}' failed:\n#{output}"
end
end
end
end
end