Skip to content

Allows metadata to override the type of Rails spec #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 28, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions features/.nav
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- Changelog.md
- Upgrade.md
- RailsVersions.md (Rails versions)
- directory_structure.feature
- model_specs:
- errors_on.feature
- records.feature
Expand Down
71 changes: 71 additions & 0 deletions features/directory_structure.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
Feature: Directory Structure

Specs are usually placed in a canonical directory structure that describes
their purpose.

* Model specs reside in the `spec/models` directory
* Controller specs reside in the `spec/controllers` directory
* Request specs reside in the `spec/requests` directory
* Feature specs reside in the `spec/features` directory
* View specs reside in the `spec/views` directory
* Helper specs reside in the `spec/helpers` directory
* Mailer specs reside in the `spec/mailers` directory
* Routing specs reside in the `spec/routing` directory

If you follow this directory structure, RSpec will automatically include the
correct test support functions for each type of test.

Application developers are free to use a different directory structure, but
will need to specify the types manually by adding a `:type` metadata key (for
example, `describe WidgetsController, :type => :controller`)

Scenario: Specs in the `spec/controllers` directory automatically tagged as controller specs
Given a file named "spec/controllers/widgets_controller_spec.rb" with:
"""ruby
require "spec_helper"

describe WidgetsController do
it "responds successfully" do
get :index
expect(response.status).to eq(200)
end
end
"""
When I run `rspec spec`
Then the example should pass

Scenario: Specs in other directories must have their types specified manually
Given a file named "spec/functional/widgets_controller_spec.rb" with:
"""ruby
require "spec_helper"

describe WidgetsController, :type => :controller do
it "responds successfully" do
get :index
expect(response.status).to eq(200)
end
end
"""
When I run `rspec spec`
Then the example should pass

Scenario: Specs in canonical directories can override their types
Given a file named "spec/routing/duckduck_routing_spec.rb" with:
"""ruby
require "spec_helper"

Rails.application.routes.draw do
get "/example" => redirect("http://example.com")
end

# Due to limitations in the Rails routing test framework, routes that
# perform redirects must actually be tested via request specs
describe "/example", :type => :request do
it "redirects to example.com" do
get "/example"
expect(response).to redirect_to("http://example.com")
end
end
"""
When I run `rspec spec`
Then the example should pass
79 changes: 55 additions & 24 deletions lib/rspec/rails/example.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,61 @@ 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])
}
if defined?(RSpec::Rails::MailerExampleGroup)
c.include RSpec::Rails::MailerExampleGroup, :type => :mailer, :example_group => {
:file_path => c.escaped_path(%w[spec mailers])
controller_path_regex = c.escaped_path(%w[spec controllers])
c.include RSpec::Rails::ControllerExampleGroup,
:type => :controller,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && controller_path_regex =~ example_group[:file_path]
}

helper_path_regex = c.escaped_path(%w[spec helpers])
c.include RSpec::Rails::HelperExampleGroup,
:type => :helper,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && helper_path_regex =~ example_group[:file_path]
}

mailer_path_regex = c.escaped_path(%w[spec mailers])
if defined?(RSpec::Rails::MailerExampleGroup)
c.include RSpec::Rails::MailerExampleGroup,
:type => :mailer,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && mailer_path_regex =~ example_group[:file_path]
}
end
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|api)])
}
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])
}
c.include RSpec::Rails::FeatureExampleGroup, :type => :feature, :example_group => {
:file_path => c.escaped_path(%w[spec features])
}

model_path_regex = c.escaped_path(%w[spec models])
c.include RSpec::Rails::ModelExampleGroup,
:type => :model,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && model_path_regex =~ example_group[:file_path]
}

request_path_regex = c.escaped_path(%w[spec (requests|integration|api)])
c.include RSpec::Rails::RequestExampleGroup,
:type => :request,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && request_path_regex =~ example_group[:file_path]
}

routing_path_regex = c.escaped_path(%w[spec routing])
c.include RSpec::Rails::RoutingExampleGroup,
:type => :routing,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && routing_path_regex =~ example_group[:file_path]
}

view_path_regex = c.escaped_path(%w[spec views])
c.include RSpec::Rails::ViewExampleGroup,
:type => :view,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && view_path_regex =~ example_group[:file_path]
}

feature_example_regex = c.escaped_path(%w[spec features])
c.include RSpec::Rails::FeatureExampleGroup,
:type => :feature,
:example_group => lambda { |example_group, metadata|
metadata[:type].nil? && feature_example_regex =~ example_group[:file_path]
}
end