Skip to content

Commit

Permalink
Include rails-specific example group extensions based on :type key.
Browse files Browse the repository at this point in the history
- e.g. describe "something", :type => :controller do
- reworked internal module inclusion functionality
- include request spec extensions in spec/integration for ease of
  upgrade from rspec-rails-1.x
- Closes #256.
  • Loading branch information
dchelimsky committed Jan 1, 2011
1 parent c3c258a commit fc5cdbb
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 34 deletions.
1 change: 0 additions & 1 deletion lib/rspec/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@
require 'rspec/rails/browser_simulators'
require 'rspec/rails/example'


30 changes: 29 additions & 1 deletion lib/rspec/rails/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,32 @@
require 'rspec/rails/example/view_example_group'
require 'rspec/rails/example/mailer_example_group'
require 'rspec/rails/example/routing_example_group'
require 'rspec/rails/example/model_example_group'
require 'rspec/rails/example/model_example_group'

RSpec::configure do |c|
def c.escaped_path(*parts)
Regexp.compile(parts.join('[\\\/]'))
end

c.include RSpec::Rails::ControllerExampleGroup, :type => :controller, :example_group => {
:file_path => c.escaped_path(%w[spec controllers])
}
c.include RSpec::Rails::HelperExampleGroup, :type => :helper, :example_group => {
:file_path => c.escaped_path(%w[spec helpers])
}
c.include RSpec::Rails::MailerExampleGroup, :type => :mailer, :example_group => {
:file_path => c.escaped_path(%w[spec mailers])
}
c.include RSpec::Rails::ModelExampleGroup, :type => :model, :example_group => {
:file_path => c.escaped_path(%w[spec models])
}
c.include RSpec::Rails::RequestExampleGroup, :type => :request, :example_group => {
:file_path => c.escaped_path(%w[spec (requests|integration)])
}
c.include RSpec::Rails::RoutingExampleGroup, :type => :routing, :example_group => {
:file_path => c.escaped_path(%w[spec routing])
}
c.include RSpec::Rails::ViewExampleGroup, :type => :view, :example_group => {
:file_path => c.escaped_path(%w[spec views])
}
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/controller_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,9 @@ module RSpec::Rails
#
module ControllerExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

include ActionController::TestCase::Behavior
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::ViewRendering
include RSpec::Rails::Matchers::RedirectTo
include RSpec::Rails::Matchers::RenderTemplate
Expand Down Expand Up @@ -172,7 +170,5 @@ module InstanceMethods
end
end
end

RSpec.configure &include_self_when_dir_matches('spec','controllers')
end
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/helper_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ module RSpec::Rails
#
module HelperExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

include ActionView::TestCase::Behavior
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::ViewAssigns
include RSpec::Rails::BrowserSimulators

Expand Down Expand Up @@ -69,7 +67,5 @@ def _controller_path
controller.controller_path = _controller_path
end
end

RSpec.configure &include_self_when_dir_matches('spec','helpers')
end
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/mailer_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
module RSpec::Rails
module MailerExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

include ActionMailer::TestCase::Behavior
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::BrowserSimulators

webrat do
Expand All @@ -29,8 +27,6 @@ def mailer_class
describes
end
end

RSpec.configure &include_self_when_dir_matches('spec','mailers')
end
end
end
3 changes: 0 additions & 3 deletions lib/rspec/rails/example/model_example_group.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
module RSpec::Rails
module ModelExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

included do
metadata[:type] = :model
end

RSpec.configure &include_self_when_dir_matches('spec','models')
end
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/request_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ module RSpec::Rails
# => delegates to assert_redirected_to(destination)
module RequestExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

include ActionDispatch::Integration::Runner
include ActionDispatch::Assertions
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::BrowserSimulators

module InstanceMethods
Expand Down Expand Up @@ -62,7 +60,5 @@ def last_response
end
end
end

RSpec.configure &include_self_when_dir_matches('spec','requests')
end
end
6 changes: 1 addition & 5 deletions lib/rspec/rails/example/routing_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
module RSpec::Rails
module RoutingExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup

include ActionDispatch::Assertions::RoutingAssertions
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::Matchers::RoutingMatchers

module InstanceMethods
Expand All @@ -27,7 +25,5 @@ def method_missing(m, *args, &block)
@routes = ::Rails.application.routes
end
end

RSpec.configure &include_self_when_dir_matches('spec','routing')
end
end
5 changes: 1 addition & 4 deletions lib/rspec/rails/example/view_example_group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@ module RSpec::Rails
# end
module ViewExampleGroup
extend ActiveSupport::Concern
extend RSpec::Rails::ModuleInclusion

include RSpec::Rails::RailsExampleGroup
include ActionView::TestCase::Behavior
include RSpec::Rails::RailsExampleGroup
include RSpec::Rails::ViewAssigns
include RSpec::Rails::Matchers::RenderTemplate
include RSpec::Rails::BrowserSimulators
Expand Down Expand Up @@ -154,8 +153,6 @@ def _include_controller_helpers
controller.request.path_parameters["action"] = _inferred_action unless _inferred_action =~ /^_/
end
end

RSpec.configure &include_self_when_dir_matches('spec','views')
end
end

2 changes: 2 additions & 0 deletions spec/rspec/rails/example/request_example_group_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
module RSpec::Rails
describe RequestExampleGroup do
it { should be_included_in_files_in('./spec/requests/') }
it { should be_included_in_files_in('./spec/integration/') }
it { should be_included_in_files_in('.\\spec\\requests\\') }
it { should be_included_in_files_in('.\\spec\\integration\\') }

it "adds :type => :request to the metadata" do
group = RSpec::Core::ExampleGroup.describe do
Expand Down

0 comments on commit fc5cdbb

Please sign in to comment.