From f3366b69f2ae6d3409cdb5fef9aaa8363c167a47 Mon Sep 17 00:00:00 2001 From: Joe Ferris Date: Thu, 10 Jun 2010 13:37:51 -0400 Subject: [PATCH] Separated unit and acceptance tests to isolate Rails-related dependencies --- Rakefile | 20 ++++++-- spec/acceptance/acceptance_helper.rb | 16 ++++++ .../acceptance_spec.rb} | 3 +- spec/{ => acceptance}/models.rb | 0 .../syntax/blueprint_spec.rb | 2 +- .../syntax/generate_spec.rb | 2 +- .../syntax/make_spec.rb | 2 +- .../syntax/sham_spec.rb | 2 +- spec/factory_girl/aliases_spec.rb | 2 +- .../attribute/association_spec.rb | 2 +- spec/factory_girl/attribute/callback_spec.rb | 2 +- spec/factory_girl/attribute/dynamic_spec.rb | 2 +- spec/factory_girl/attribute/static_spec.rb | 2 +- spec/factory_girl/attribute_spec.rb | 2 +- spec/factory_girl/factory_spec.rb | 13 ++++- .../factory_girl/proxy/attributes_for_spec.rb | 2 +- spec/factory_girl/proxy/build_spec.rb | 2 +- spec/factory_girl/proxy/create_spec.rb | 2 +- spec/factory_girl/proxy/stub_spec.rb | 2 +- spec/factory_girl/proxy_spec.rb | 2 +- spec/factory_girl/sequence_spec.rb | 2 +- spec/spec_helper.rb | 50 ++++++++++++------- 22 files changed, 93 insertions(+), 41 deletions(-) create mode 100644 spec/acceptance/acceptance_helper.rb rename spec/{integration_spec.rb => acceptance/acceptance_spec.rb} (99%) rename spec/{ => acceptance}/models.rb (100%) rename spec/{factory_girl => acceptance}/syntax/blueprint_spec.rb (91%) rename spec/{factory_girl => acceptance}/syntax/generate_spec.rb (94%) rename spec/{factory_girl => acceptance}/syntax/make_spec.rb (90%) rename spec/{factory_girl => acceptance}/syntax/sham_spec.rb (91%) diff --git a/Rakefile b/Rakefile index 3fb252e..7061d5b 100644 --- a/Rakefile +++ b/Rakefile @@ -9,15 +9,29 @@ require 'cucumber/rake/task' desc 'Default: run the specs and features.' task :default do + system("rake -s spec:unit;") %w(2.1 2.3 3.0).each do |version| - system("RAILS_VERSION=#{version} rake -s spec features;") + system("RAILS_VERSION=#{version} rake -s spec:acceptance features;") end end -Spec::Rake::SpecTask.new do |t| - t.spec_opts = ['--options', "spec/spec.opts"] +namespace :spec do + desc "Run unit specs" + Spec::Rake::SpecTask.new('unit') do |t| + t.spec_opts = ['--options', "spec/spec.opts"] + t.spec_files = FileList['spec/factory_girl/**/*_spec.rb'] + end + + desc "Run acceptance specs" + Spec::Rake::SpecTask.new('acceptance') do |t| + t.spec_opts = ['--options', "spec/spec.opts"] + t.spec_files = FileList['spec/acceptance/**/*_spec.rb'] + end end +desc "Run the unit and acceptance specs" +task :spec => ['spec:unit', 'spec:acceptance'] + desc 'Performs code coverage on the factory_girl plugin.' Rcov::RcovTask.new do |t| t.test_files = FileList['spec/*_spec.rb'] diff --git a/spec/acceptance/acceptance_helper.rb b/spec/acceptance/acceptance_helper.rb new file mode 100644 index 0000000..1da22e1 --- /dev/null +++ b/spec/acceptance/acceptance_helper.rb @@ -0,0 +1,16 @@ +case ENV['RAILS_VERSION'] +when '2.1' then + gem 'activerecord', '~>2.1.0' +when '3.0' then + gem 'activerecord', '~>3.0.0' +else + gem 'activerecord', '~>2.3.0' +end + +require 'active_record' +require 'active_record/version' + +puts "Running specs using Rails #{ActiveRecord::VERSION::STRING}" + +require 'acceptance/models' + diff --git a/spec/integration_spec.rb b/spec/acceptance/acceptance_spec.rb similarity index 99% rename from spec/integration_spec.rb rename to spec/acceptance/acceptance_spec.rb index eedb628..74fa465 100644 --- a/spec/integration_spec.rb +++ b/spec/acceptance/acceptance_spec.rb @@ -1,4 +1,5 @@ -require File.expand_path(File.join(File.dirname(__FILE__), 'spec_helper')) +require 'spec_helper' +require 'acceptance/acceptance_helper' describe "integration" do before do diff --git a/spec/models.rb b/spec/acceptance/models.rb similarity index 100% rename from spec/models.rb rename to spec/acceptance/models.rb diff --git a/spec/factory_girl/syntax/blueprint_spec.rb b/spec/acceptance/syntax/blueprint_spec.rb similarity index 91% rename from spec/factory_girl/syntax/blueprint_spec.rb rename to spec/acceptance/syntax/blueprint_spec.rb index 5b6802c..d6e9605 100644 --- a/spec/factory_girl/syntax/blueprint_spec.rb +++ b/spec/acceptance/syntax/blueprint_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') +require 'spec_helper' require 'factory_girl/syntax/blueprint' diff --git a/spec/factory_girl/syntax/generate_spec.rb b/spec/acceptance/syntax/generate_spec.rb similarity index 94% rename from spec/factory_girl/syntax/generate_spec.rb rename to spec/acceptance/syntax/generate_spec.rb index f55bb3c..4aa806e 100644 --- a/spec/factory_girl/syntax/generate_spec.rb +++ b/spec/acceptance/syntax/generate_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' require 'factory_girl/syntax/generate' diff --git a/spec/factory_girl/syntax/make_spec.rb b/spec/acceptance/syntax/make_spec.rb similarity index 90% rename from spec/factory_girl/syntax/make_spec.rb rename to spec/acceptance/syntax/make_spec.rb index 8fbc9fe..e974d35 100644 --- a/spec/factory_girl/syntax/make_spec.rb +++ b/spec/acceptance/syntax/make_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') +require 'spec_helper' require 'factory_girl/syntax/make' diff --git a/spec/factory_girl/syntax/sham_spec.rb b/spec/acceptance/syntax/sham_spec.rb similarity index 91% rename from spec/factory_girl/syntax/sham_spec.rb rename to spec/acceptance/syntax/sham_spec.rb index 78900a7..2f638be 100644 --- a/spec/factory_girl/syntax/sham_spec.rb +++ b/spec/acceptance/syntax/sham_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.dirname(__FILE__) + '/../../spec_helper') +require 'spec_helper' require 'factory_girl/syntax/sham' diff --git a/spec/factory_girl/aliases_spec.rb b/spec/factory_girl/aliases_spec.rb index 28ccf6d..ad4a9d9 100644 --- a/spec/factory_girl/aliases_spec.rb +++ b/spec/factory_girl/aliases_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +require 'spec_helper' describe Factory, "aliases" do diff --git a/spec/factory_girl/attribute/association_spec.rb b/spec/factory_girl/attribute/association_spec.rb index e5fa682..d4ac524 100644 --- a/spec/factory_girl/attribute/association_spec.rb +++ b/spec/factory_girl/attribute/association_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Attribute::Association do before do diff --git a/spec/factory_girl/attribute/callback_spec.rb b/spec/factory_girl/attribute/callback_spec.rb index d49e3f8..22181dd 100644 --- a/spec/factory_girl/attribute/callback_spec.rb +++ b/spec/factory_girl/attribute/callback_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Attribute::Callback do before do diff --git a/spec/factory_girl/attribute/dynamic_spec.rb b/spec/factory_girl/attribute/dynamic_spec.rb index 32ef54e..751060a 100644 --- a/spec/factory_girl/attribute/dynamic_spec.rb +++ b/spec/factory_girl/attribute/dynamic_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Attribute::Dynamic do before do diff --git a/spec/factory_girl/attribute/static_spec.rb b/spec/factory_girl/attribute/static_spec.rb index 4c45b02..9b596fd 100644 --- a/spec/factory_girl/attribute/static_spec.rb +++ b/spec/factory_girl/attribute/static_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Attribute::Static do before do diff --git a/spec/factory_girl/attribute_spec.rb b/spec/factory_girl/attribute_spec.rb index 585c022..b3b13a4 100644 --- a/spec/factory_girl/attribute_spec.rb +++ b/spec/factory_girl/attribute_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +require 'spec_helper' describe Factory::Attribute do before do diff --git a/spec/factory_girl/factory_spec.rb b/spec/factory_girl/factory_spec.rb index dd73b1f..5478d56 100644 --- a/spec/factory_girl/factory_spec.rb +++ b/spec/factory_girl/factory_spec.rb @@ -1,6 +1,15 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +require 'spec_helper' describe Factory do + include DefinesConstants + + before do + define_constant('User') + define_constant('Admin', User) + define_constant('Business') + define_constant('Admin::Settings') + end + describe "defining a factory" do before do @name = :user @@ -411,7 +420,7 @@ describe Factory do end it "should build Admin::Settings class from Admin::Settings string" do - factory = Factory.define(@name.to_s, :class => @class.name.underscore) {} + factory = Factory.define(@name.to_s, :class => 'admin/settings') {} factory.build_class.should == @class end end diff --git a/spec/factory_girl/proxy/attributes_for_spec.rb b/spec/factory_girl/proxy/attributes_for_spec.rb index 6026d28..ca6af33 100644 --- a/spec/factory_girl/proxy/attributes_for_spec.rb +++ b/spec/factory_girl/proxy/attributes_for_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Proxy::AttributesFor do before do diff --git a/spec/factory_girl/proxy/build_spec.rb b/spec/factory_girl/proxy/build_spec.rb index 40efb06..ad9a45e 100644 --- a/spec/factory_girl/proxy/build_spec.rb +++ b/spec/factory_girl/proxy/build_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Proxy::Build do before do diff --git a/spec/factory_girl/proxy/create_spec.rb b/spec/factory_girl/proxy/create_spec.rb index af87574..5ab9656 100644 --- a/spec/factory_girl/proxy/create_spec.rb +++ b/spec/factory_girl/proxy/create_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Proxy::Create do before do diff --git a/spec/factory_girl/proxy/stub_spec.rb b/spec/factory_girl/proxy/stub_spec.rb index f0dfbcb..cea255a 100644 --- a/spec/factory_girl/proxy/stub_spec.rb +++ b/spec/factory_girl/proxy/stub_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'spec_helper')) +require 'spec_helper' describe Factory::Proxy::Stub do before do diff --git a/spec/factory_girl/proxy_spec.rb b/spec/factory_girl/proxy_spec.rb index b43cbfc..aa7b772 100644 --- a/spec/factory_girl/proxy_spec.rb +++ b/spec/factory_girl/proxy_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +require 'spec_helper' describe Factory::Proxy do before do diff --git a/spec/factory_girl/sequence_spec.rb b/spec/factory_girl/sequence_spec.rb index 5a2f02e..c43aaaa 100644 --- a/spec/factory_girl/sequence_spec.rb +++ b/spec/factory_girl/sequence_spec.rb @@ -1,4 +1,4 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'spec_helper')) +require 'spec_helper' describe Factory::Sequence do describe "a sequence" do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index dbfaccb..68f1a1f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,30 +1,42 @@ -$: << File.join(File.dirname(__FILE__), '..', 'lib') -$: << File.join(File.dirname(__FILE__)) +$LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib') +$LOAD_PATH << File.join(File.dirname(__FILE__)) require 'rubygems' - -case ENV['RAILS_VERSION'] -when '2.1' then - gem 'activerecord', '~>2.1.0' -when '3.0' then - gem 'activerecord', '~>3.0.0' -else - gem 'activerecord', '~>2.3.0' -end - -require 'active_record' -require 'active_record/version' - -puts "Running specs using Rails #{ActiveRecord::VERSION::STRING}" - require 'spec' require 'spec/autorun' require 'rr' -require 'models' - require 'factory_girl' Spec::Runner.configure do |config| config.mock_with RR::Adapters::Rspec end + +share_as :DefinesConstants do + before do + @defined_constants = [] + end + + after do + @defined_constants.reverse.each do |path| + namespace, class_name = *constant_path(path) + namespace.send(:remove_const, class_name) + end + end + + def define_constant(path, base = Object, &block) + namespace, class_name = *constant_path(path) + klass = Class.new(base) + namespace.const_set(class_name, klass) + klass.class_eval(&block) if block_given? + @defined_constants << path + klass + end + + def constant_path(constant_name) + names = constant_name.split('::') + class_name = names.pop + namespace = names.inject(Object) { |result, name| result.const_get(name) } + [namespace, class_name] + end +end