Permalink
Browse files

Tweak alternate app integration

This commit removes most if-statements from the example groups,
consolidating to a global method called `at_least_rails_3_1`. It
also adds filters to the rspec tests and changes engine_support.rb
to application.rb.
  • Loading branch information...
1 parent acd46c2 commit 1c451f8cd381c9a602afc249ea32a5057adffaba @danrasband danrasband committed Jun 4, 2012
View
@@ -5,6 +5,10 @@
c.backtrace_clean_patterns << /lib\/rspec\/rails/
end
+def at_least_rails_3_1?
+ Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+end
+
require 'rspec/rails/extensions'
require 'rspec/rails/view_rendering'
require 'rspec/rails/adapters'
@@ -15,4 +19,4 @@
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
require 'rspec/rails/vendor/webrat'
-require 'rspec/rails/engine_support'
+require 'rspec/rails/application'
@@ -0,0 +1,13 @@
+module RSpec
+ module Rails
+ module Application
+ RSpec.configuration.add_setting :application, :default => ::Rails.application
+
+ # unless at_least_rails_3_1?
+ # # def RSpec.configuration.application=(*)
+ # # raise "Setting the application is only supported on Rails 3.1 and above."
+ # # end
+ # end
+ end
+ end
+end
@@ -1,11 +0,0 @@
-if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- module RSpec
- module Rails
- module EngineSupport
- RSpec::configure do |config|
- config.add_setting :application, :default => ::Rails.application
- end
- end
- end
- end
-end
@@ -120,11 +120,7 @@ def method_missing(method, *args, &block)
metadata[:type] = :controller
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @routes = RSpec.configuration.application.routes
- else
- @routes = ::Rails.application.routes
- end
+ @routes = RSpec.configuration.application.routes
ActionController::Base.allow_forgery_protection = false
end
end
@@ -7,11 +7,7 @@ module MailerExampleGroup
included do
metadata[:type] = :mailer
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- include RSpec.configuration.application.routes.url_helpers
- else
- include ::Rails.application.routes.url_helpers
- end
+ include RSpec.configuration.application.routes.url_helpers
options = ::Rails.configuration.action_mailer.default_url_options
options.each { |key, value| default_url_options[key] = value } if options
end
@@ -9,22 +9,14 @@ module RequestExampleGroup
include ActionController::TemplateAssertions
def app
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- return RSpec.configuration.application
- else
- return ::Rails.application
- end
+ return RSpec.configuration.application
end
included do
metadata[:type] = :request
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @routes = RSpec.configuration.application.routes
- else
- @routes = ::Rails.application.routes
- end
+ @routes = RSpec.configuration.application.routes
end
end
end
@@ -12,11 +12,7 @@ module RoutingExampleGroup
metadata[:type] = :routing
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @routes = RSpec.configuration.application.routes
- else
- @routes = ::Rails.application.routes
- end
+ @routes = RSpec.configuration.application.routes
end
end
@@ -29,35 +29,23 @@
context "default" do
it "is Rails.application by default" do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application.should eq(::Rails.application)
- else
- expect { RSpec.configuration.application }.should raise_error
- end
+ RSpec.configuration.application.should eq(::Rails.application)
end
end
- context "custom rack application" do
+ context "custom rack application", :at_least_rails_3_1 do
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @orig_application = RSpec.configuration.application
- end
+ @orig_application = RSpec.configuration.application
end
after do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = @orig_application
- end
+ RSpec.configuration.application = @orig_application
end
it "allows for custom application" do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = RSpec::EngineExample
- RSpec.configuration.application.should eq(RSpec::EngineExample)
- else
- expect { RSpec.configuration.application = RSpec::EngineExample }.should raise_error
- end
+ RSpec.configuration.application = RSpec::EngineExample
+ RSpec.configuration.application.should eq(RSpec::EngineExample)
end
end
@@ -97,18 +97,14 @@ module RSpec::Rails
end
end
- describe "#application" do
+ describe "#application", :at_least_rails_3_1 do
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
- end
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
end
after do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = @orig_application
- end
+ RSpec.configuration.application = @orig_application
end
it "still delegates name routes to underlying controller" do
@@ -118,13 +114,8 @@ module RSpec::Rails
example = group.new
example.stub(:controller => controller)
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- example.instance_variable_set(:@orig_routes, RSpec.configuration.application.routes)
- example.bars_path.should eq('/foos')
- else
- example.instance_variable_set(:@orig_routes, ::Rails.application.routes)
- expect { example.bars_path }.should raise_error
- end
+ example.instance_variable_set(:@orig_routes, RSpec.configuration.application.routes)
+ example.bars_path.should eq('/foos')
end
end
end
@@ -18,18 +18,14 @@ module ::Rails; end
group.metadata[:type].should eq(:mailer)
end
- describe "custom application" do
+ describe "custom application", :at_least_rails_3_1 do
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
- end
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
end
after do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = @orig_application
- end
+ RSpec.configuration.application = @orig_application
end
it "should include custom application's url helpers" do
@@ -38,11 +34,7 @@ module ::Rails; end
end
example = group.new
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- example.bars_path.should == '/bars'
- else
- expect { example.bars_path }.should raise_error
- end
+ example.bars_path.should == '/bars'
end
end
end
@@ -14,18 +14,14 @@ module RSpec::Rails
group.metadata[:type].should eq(:request)
end
- describe "#app" do
+ describe "#app", :at_least_rails_3_1 do
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
- end
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
end
after do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = @orig_application
- end
+ RSpec.configuration.application = @orig_application
end
it "sets app as custom application" do
@@ -35,11 +31,7 @@ module RSpec::Rails
example = group.new
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- example.app.should eq(RSpec::EngineExample)
- else
- example.app.should eq(::Rails.application)
- end
+ example.app.should eq(RSpec::EngineExample)
end
end
end
@@ -29,18 +29,14 @@ module RSpec::Rails
end
end
- describe "custom application routes" do
+ describe "custom application routes", :at_least_rails_3_1 do
before do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
- end
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
end
after do
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- RSpec.configuration.application = @orig_application
- end
+ RSpec.configuration.application = @orig_application
end
it "provides routes of custom application" do
@@ -50,14 +46,9 @@ module RSpec::Rails
example = group.new
- if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
- # Because this relies on before hooks, I have to stub this in.
- example.stub(:routes => RSpec.configuration.application.routes)
- example.bars_path.should == "/bars"
- else
- example.stub(:routes => ::Rails.application.routes)
- expect { example.bars_path }.should raise_error
- end
+ # Because this relies on before hooks, I have to stub this in.
+ example.stub(:routes => RSpec.configuration.application.routes)
+ example.bars_path.should == "/bars"
end
end
View
@@ -16,7 +16,7 @@ def self.run_all(reporter=nil)
end
end
-if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+if at_least_rails_3_1?
RSpec::EngineExample.routes.draw do
root :to => "foo#index"
resources :bars
@@ -26,6 +26,9 @@ def self.run_all(reporter=nil)
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus
+ unless at_least_rails_3_1?
+ config.filter_run_excluding :at_least_rails_3_1
+ end
config.run_all_when_everything_filtered = true
config.before(:each) do
@real_world = RSpec.world

0 comments on commit 1c451f8

Please sign in to comment.