Skip to content

Commit

Permalink
Added configuration to allow for custom application engine.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dan Rasband committed May 10, 2012
1 parent 0677052 commit 83e24bc
Show file tree
Hide file tree
Showing 12 changed files with 129 additions and 5 deletions.
1 change: 1 addition & 0 deletions lib/rspec/rails.rb
Expand Up @@ -9,6 +9,7 @@
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'
Expand Down
9 changes: 9 additions & 0 deletions lib/rspec/rails/engine_support.rb
@@ -0,0 +1,9 @@
module RSpec
module Rails
module EngineSupport
RSpec::configure do |config|
config.add_setting :application, :default => ::Rails.application
end
end
end
end
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/controller_example_group.rb
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/mailer_example_group.rb
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/rails/example/request_example_group.rb
Expand Up @@ -9,14 +9,14 @@ module RequestExampleGroup
include ActionController::TemplateAssertions

def app
::Rails.application
RSpec.configuration.application
end

included do
metadata[:type] = :request

before do
@routes = ::Rails.application.routes
@routes = RSpec.configuration.application.routes
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec/rails/example/routing_example_group.rb
Expand Up @@ -12,7 +12,7 @@ module RoutingExampleGroup
metadata[:type] = :routing

before do
@routes = ::Rails.application.routes
@routes = RSpec.configuration.application.routes
end
end

Expand Down
13 changes: 13 additions & 0 deletions spec/rspec/rails/configuration_spec.rb
Expand Up @@ -3,10 +3,12 @@
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
Expand All @@ -23,4 +25,15 @@
RSpec.configuration.render_views?.should be_true
end
end

describe "#application" do
it "is Rails.application by default" do
RSpec.configuration.application.should eq(::Rails.application)
end

it "allows for custom application" do
RSpec.configuration.application = RSpec::EngineExample
RSpec.configuration.application.should eq(RSpec::EngineExample)
end
end
end
23 changes: 23 additions & 0 deletions spec/rspec/rails/example/controller_example_group_spec.rb
Expand Up @@ -96,5 +96,28 @@ module RSpec::Rails
controller_class.superclass.should eq(ApplicationController)
end
end

describe "#application" 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
20 changes: 20 additions & 0 deletions spec/rspec/rails/example/mailer_example_group_spec.rb
Expand Up @@ -17,5 +17,25 @@ module ::Rails; end
end
group.metadata[:type].should eq(:mailer)
end

describe "custom application" 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
21 changes: 21 additions & 0 deletions spec/rspec/rails/example/request_example_group_spec.rb
Expand Up @@ -13,5 +13,26 @@ module RSpec::Rails
end
group.metadata[:type].should eq(:request)
end

describe "#app" 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
25 changes: 25 additions & 0 deletions spec/rspec/rails/example/routing_example_group_spec.rb
Expand Up @@ -28,5 +28,30 @@ module RSpec::Rails
example.foo_path.should == "foo"
end
end

describe "custom application routes" 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
12 changes: 12 additions & 0 deletions spec/support/engine_example.rb
@@ -0,0 +1,12 @@
module RSpec
class EngineExample < ::Rails::Engine
def self.activate
end
end
end


RSpec::EngineExample.routes.draw do
root :to => "foo#index"
resources :bars
end

0 comments on commit 83e24bc

Please sign in to comment.