Skip to content
Browse files

Merge pull request #539 from danrasband/custom-application-configuration

Added configuration to allow for custom application engine.
  • Loading branch information...
2 parents 1e8148c + b694543 commit 248ce66898d8fca1d5aca4da1e09859a45680e39 @justinko justinko committed Jun 23, 2012
View
4 lib/rspec-rails.rb
@@ -1,5 +1,9 @@
module RSpec
module Rails
+ def self.at_least_rails_3_1?
+ Gem::Version.new(::Rails.version) >= Gem::Version.new('3.1.0')
+ end
+
class Railtie < ::Rails::Railtie
# Rails-3.0.1 requires config.app_generators instead of 3.0.0's config.generators
generators = config.respond_to?(:app_generators) ? config.app_generators : config.generators
View
1 lib/rspec/rails.rb
@@ -15,3 +15,4 @@
require 'rspec/rails/example'
require 'rspec/rails/vendor/capybara'
require 'rspec/rails/vendor/webrat'
+require 'rspec/rails/application'
View
18 lib/rspec/rails/application.rb
@@ -0,0 +1,18 @@
+require 'rspec-rails'
+module RSpec
+ module Rails
+ module Application
+ RSpec.configuration.add_setting :application, :default => ::Rails.application
+
+ # Raise an error when setting application in Rails < 3.1
+ unless RSpec::Rails.at_least_rails_3_1?
+ class << RSpec.configuration
+ def application=(*)
+ raise "Setting the application is only supported on Rails 3.1 and above."
+ end
+ end
+ end
+
+ end
+ end
+end
View
2 lib/rspec/rails/example/controller_example_group.rb
@@ -120,7 +120,7 @@ def method_missing(method, *args, &block)
metadata[:type] = :controller
before do
- @routes = ::Rails.application.routes
+ @routes = RSpec.configuration.application.routes
ActionController::Base.allow_forgery_protection = false
end
end
View
2 lib/rspec/rails/example/mailer_example_group.rb
@@ -7,7 +7,7 @@ module MailerExampleGroup
included do
metadata[:type] = :mailer
- include ::Rails.application.routes.url_helpers
+ 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
View
4 lib/rspec/rails/example/request_example_group.rb
@@ -9,14 +9,14 @@ module RequestExampleGroup
include ActionController::TemplateAssertions
def app
- ::Rails.application
+ return RSpec.configuration.application
end
included do
metadata[:type] = :request
before do
- @routes = ::Rails.application.routes
+ @routes = RSpec.configuration.application.routes
end
end
end
View
2 lib/rspec/rails/example/routing_example_group.rb
@@ -12,7 +12,7 @@ module RoutingExampleGroup
metadata[:type] = :routing
before do
- @routes = ::Rails.application.routes
+ @routes = RSpec.configuration.application.routes
end
end
View
31 spec/rspec/rails/configuration_spec.rb
@@ -23,4 +23,35 @@
RSpec.configuration.render_views?.should be_true
end
end
+
+ describe "#application" do
+
+ context "default" do
+
+ it "is Rails.application by default" do
+ RSpec.configuration.application.should eq(::Rails.application)
+ end
+
+ it "should raise an error for Rails 3.0", :not_at_least_rails_3_1 do
+ expect { RSpec.configuration.application = ::Rails.application }.should raise_error
+ end
+
+ end
+
+ context "custom rack application", :at_least_rails_3_1 do
+ before do
+ @orig_application = RSpec.configuration.application
+ end
+
+ after do
+ RSpec.configuration.application = @orig_application
+ end
+
+ it "allows for custom application" do
+ RSpec.configuration.application = RSpec::EngineExample
+ RSpec.configuration.application.should eq(RSpec::EngineExample)
+ end
+
+ end
+ end
end
View
22 spec/rspec/rails/example/controller_example_group_spec.rb
@@ -96,5 +96,27 @@ module RSpec::Rails
controller_class.superclass.should eq(ApplicationController)
end
end
+
+ describe "#application", :at_least_rails_3_1 do
+ before do
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
+
+ after do
+ RSpec.configuration.application = @orig_application
+ end
+
+ it "still delegates name routes to underlying controller" do
+ controller = double('controller')
+ controller.stub(:bars_path).and_return('/foos')
+
+ example = group.new
+ example.stub(:controller => controller)
+
+ example.instance_variable_set(:@orig_routes, RSpec.configuration.application.routes)
+ example.bars_path.should eq('/foos')
+ end
+ end
end
end
View
20 spec/rspec/rails/example/mailer_example_group_spec.rb
@@ -17,5 +17,25 @@ module ::Rails; end
end
group.metadata[:type].should eq(:mailer)
end
+
+ describe "custom application", :at_least_rails_3_1 do
+ before do
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
+
+ after do
+ RSpec.configuration.application = @orig_application
+ end
+
+ it "should include custom application's url helpers" do
+ group = RSpec::Core::ExampleGroup.describe do
+ include MailerExampleGroup
+ end
+
+ example = group.new
+ example.bars_path.should == '/bars'
+ end
+ end
end
end
View
21 spec/rspec/rails/example/request_example_group_spec.rb
@@ -13,5 +13,26 @@ module RSpec::Rails
end
group.metadata[:type].should eq(:request)
end
+
+ describe "#app", :at_least_rails_3_1 do
+ before do
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
+
+ after do
+ RSpec.configuration.application = @orig_application
+ end
+
+ it "sets app as custom application" do
+ group = RSpec::Core::ExampleGroup.describe do
+ include RequestExampleGroup
+ end
+
+ example = group.new
+
+ example.app.should eq(RSpec::EngineExample)
+ end
+ end
end
end
View
24 spec/rspec/rails/example/routing_example_group_spec.rb
@@ -28,5 +28,29 @@ module RSpec::Rails
example.foo_path.should == "foo"
end
end
+
+ describe "custom application routes", :at_least_rails_3_1 do
+ before do
+ @orig_application = RSpec.configuration.application
+ RSpec.configuration.application = RSpec::EngineExample
+ end
+
+ after do
+ RSpec.configuration.application = @orig_application
+ end
+
+ it "provides routes of custom application" do
+ group = RSpec::Core::ExampleGroup.describe do
+ include RoutingExampleGroup
+ end
+
+ 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"
+ end
+
+ end
end
end
View
12 spec/spec_helper.rb
@@ -16,9 +16,21 @@ def self.run_all(reporter=nil)
end
end
+if RSpec::Rails.at_least_rails_3_1?
+ 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
+ if RSpec::Rails.at_least_rails_3_1?
+ config.filter_run_excluding :not_at_least_rails_3_1
+ else
+ 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
View
6 spec/support/engine_example.rb
@@ -0,0 +1,6 @@
+module RSpec
+ class EngineExample < ::Rails::Engine
+ def self.activate
+ end
+ end
+end

0 comments on commit 248ce66

Please sign in to comment.
Something went wrong with that request. Please try again.