Permalink
Browse files

Make code version specific for rails 3.1 and up

This commit adds Rails version checking to the RSpec application
configuration to handle Rails 3.0.x. The Rails::Engine class, while
available in Rails 3.0, does not have the capability to draw routes as
it does in 3.1 and above.
  • Loading branch information...
1 parent 83e24bc commit acd46c25348cf1cc71747aa04b192b9419adedc7 @danrasband danrasband committed May 16, 2012
View
@@ -9,10 +9,10 @@
require 'rspec/rails/view_rendering'
require 'rspec/rails/adapters'
require 'rspec/rails/matchers'
-require 'rspec/rails/engine_support'
require 'rspec/rails/fixture_support'
require 'rspec/rails/mocks'
require 'rspec/rails/module_inclusion'
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
require 'rspec/rails/vendor/webrat'
+require 'rspec/rails/engine_support'
@@ -1,8 +1,10 @@
-module RSpec
- module Rails
- module EngineSupport
- RSpec::configure do |config|
- config.add_setting :application, :default => ::Rails.application
+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
@@ -120,7 +120,11 @@ def method_missing(method, *args, &block)
metadata[:type] = :controller
before do
- @routes = RSpec.configuration.application.routes
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @routes = RSpec.configuration.application.routes
+ else
+ @routes = ::Rails.application.routes
+ end
ActionController::Base.allow_forgery_protection = false
end
end
@@ -7,7 +7,11 @@ module MailerExampleGroup
included do
metadata[:type] = :mailer
- include RSpec.configuration.application.routes.url_helpers
+ 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
options = ::Rails.configuration.action_mailer.default_url_options
options.each { |key, value| default_url_options[key] = value } if options
end
@@ -9,14 +9,22 @@ module RequestExampleGroup
include ActionController::TemplateAssertions
def app
- RSpec.configuration.application
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ return RSpec.configuration.application
+ else
+ return ::Rails.application
+ end
end
included do
metadata[:type] = :request
before do
- @routes = RSpec.configuration.application.routes
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @routes = RSpec.configuration.application.routes
+ else
+ @routes = ::Rails.application.routes
+ end
end
end
end
@@ -12,7 +12,11 @@ module RoutingExampleGroup
metadata[:type] = :routing
before do
- @routes = RSpec.configuration.application.routes
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @routes = RSpec.configuration.application.routes
+ else
+ @routes = ::Rails.application.routes
+ end
end
end
@@ -3,12 +3,10 @@
describe "configuration" do
before do
@orig_render_views = RSpec.configuration.render_views?
- @orig_application = RSpec.configuration.application
end
after do
RSpec.configuration.render_views = @orig_render_views
- RSpec.configuration.application = @orig_application
end
describe "#render_views?" do
@@ -27,13 +25,41 @@
end
describe "#application" do
- it "is Rails.application by default" do
- RSpec.configuration.application.should eq(::Rails.application)
+
+ 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
+ end
+
end
- it "allows for custom application" do
- RSpec.configuration.application = RSpec::EngineExample
- RSpec.configuration.application.should eq(RSpec::EngineExample)
+ context "custom rack application" do
+ before do
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @orig_application = RSpec.configuration.application
+ end
+ end
+
+ after do
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec.configuration.application = @orig_application
+ end
+ 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
+ end
+
end
end
end
@@ -99,12 +99,16 @@ module RSpec::Rails
describe "#application" do
before do
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
end
after do
- RSpec.configuration.application = @orig_application
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec.configuration.application = @orig_application
+ end
end
it "still delegates name routes to underlying controller" do
@@ -114,9 +118,13 @@ module RSpec::Rails
example = group.new
example.stub(:controller => controller)
- example.instance_variable_set(:@orig_routes, RSpec.configuration.application.routes)
-
- example.bars_path.should eq('/foos')
+ 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
end
end
end
@@ -20,12 +20,16 @@ module ::Rails; end
describe "custom application" do
before do
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
end
after do
- RSpec.configuration.application = @orig_application
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec.configuration.application = @orig_application
+ end
end
it "should include custom application's url helpers" do
@@ -34,7 +38,11 @@ module ::Rails; end
end
example = group.new
- example.bars_path.should == '/bars'
+ 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
end
end
end
@@ -16,12 +16,16 @@ module RSpec::Rails
describe "#app" do
before do
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
end
after do
- RSpec.configuration.application = @orig_application
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec.configuration.application = @orig_application
+ end
end
it "sets app as custom application" do
@@ -31,7 +35,11 @@ module RSpec::Rails
example = group.new
- example.app.should eq(RSpec::EngineExample)
+ 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
end
end
end
@@ -31,12 +31,16 @@ module RSpec::Rails
describe "custom application routes" do
before do
- @orig_application = RSpec.configuration.application
- RSpec.configuration.application = RSpec::EngineExample
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
end
after do
- RSpec.configuration.application = @orig_application
+ if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec.configuration.application = @orig_application
+ end
end
it "provides routes of custom application" do
@@ -46,10 +50,14 @@ module RSpec::Rails
example = group.new
- # Because this relies on before hooks, I have to stub this in.
- example.stub(:routes => RSpec.configuration.application.routes)
-
- example.bars_path.should == "/bars"
+ 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
end
end
View
@@ -16,6 +16,13 @@ def self.run_all(reporter=nil)
end
end
+if Gem::Version.new(Rails.version) >= Gem::Version.new('3.1.0')
+ RSpec::EngineExample.routes.draw do
+ root :to => "foo#index"
+ resources :bars
+ end
+end
+
RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.filter_run :focus
@@ -4,9 +4,3 @@ def self.activate
end
end
end
-
-
-RSpec::EngineExample.routes.draw do
- root :to => "foo#index"
- resources :bars
-end

0 comments on commit acd46c2

Please sign in to comment.