Skip to content

Commit 041f542

Browse files
committed
Merge pull request #970 from rspec/issue-662
Make filetype infer opt-in
2 parents 3d63b16 + 0223ebd commit 041f542

File tree

5 files changed

+88
-0
lines changed

5 files changed

+88
-0
lines changed

Changelog.md

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ Breaking Changes for 3.0.0:
55

66
* Extracts the `mock_model` and `stub_model` methods to the
77
`rspec-activemodel-mocks` gem. (Thomas Holmes)
8+
* Spec types are no longer inferred by location, they instead need to be
9+
explicitly tagged. The old behaviour is enabled by
10+
`config.infer_spec_type_from_file_location!`, which is still supplied
11+
in the default generated `spec_helper.rb`. (Xavier Shay)
812

913
### 3.0.0.beta2 / 2014-02-17
1014
[Full Changelog](https://github.com/rspec/rspec-rails/compare/v3.0.0.beta1...v3.0.0.beta2)

lib/generators/rspec/install/templates/spec/spec_helper.rb.tt

+15
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,19 @@ RSpec.configure do |config|
4545
# the seed, which is printed after each run.
4646
# --seed 1234
4747
config.order = "random"
48+
49+
# RSpec Rails can automatically mix in different behaviours to your tests
50+
# based on their file location, for example enabling you to call `get` and
51+
# `post` in specs under `spec/controllers`.
52+
#
53+
# You can disable this behaviour by removing the line below, and instead
54+
# explictly tag your specs with their type, e.g.:
55+
#
56+
# describe UsersController, :type => :controller do
57+
# # ...
58+
# end
59+
#
60+
# The different available types are documented in the features, such as in
61+
# https://relishapp.com/rspec/rspec-rails/v/3-0/docs
62+
config.infer_spec_type_from_file_location!
4863
end

lib/rspec/rails/example.rb

+2
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@
77
require 'rspec/rails/example/routing_example_group'
88
require 'rspec/rails/example/model_example_group'
99
require 'rspec/rails/example/feature_example_group'
10+
11+
require 'rspec/rails/infer_type_configuration'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
RSpec.configure do |config|
2+
def config.infer_spec_type_from_file_location!
3+
def self.escaped_path(*parts)
4+
Regexp.compile(parts.join('[\\\/]') + '[\\\/]')
5+
end
6+
7+
controller_path_regex = self.escaped_path(%w[spec controllers])
8+
self.include RSpec::Rails::ControllerExampleGroup,
9+
:type => :controller,
10+
:file_path => lambda { |file_path, metadata|
11+
metadata[:type].nil? && controller_path_regex =~ file_path
12+
}
13+
14+
helper_path_regex = self.escaped_path(%w[spec helpers])
15+
self.include RSpec::Rails::HelperExampleGroup,
16+
:type => :helper,
17+
:file_path => lambda { |file_path, metadata|
18+
metadata[:type].nil? && helper_path_regex =~ file_path
19+
}
20+
21+
mailer_path_regex = self.escaped_path(%w[spec mailers])
22+
if defined?(RSpec::Rails::MailerExampleGroup)
23+
self.include RSpec::Rails::MailerExampleGroup,
24+
:type => :mailer,
25+
:file_path => lambda { |file_path, metadata|
26+
metadata[:type].nil? && mailer_path_regex =~ file_path
27+
}
28+
end
29+
30+
model_path_regex = self.escaped_path(%w[spec models])
31+
self.include RSpec::Rails::ModelExampleGroup,
32+
:type => :model,
33+
:file_path => lambda { |file_path, metadata|
34+
metadata[:type].nil? && model_path_regex =~ file_path
35+
}
36+
37+
request_path_regex = self.escaped_path(%w[spec (requests|integration|api)])
38+
self.include RSpec::Rails::RequestExampleGroup,
39+
:type => :request,
40+
:file_path => lambda { |file_path, metadata|
41+
metadata[:type].nil? && request_path_regex =~ file_path
42+
}
43+
44+
routing_path_regex = self.escaped_path(%w[spec routing])
45+
self.include RSpec::Rails::RoutingExampleGroup,
46+
:type => :routing,
47+
:file_path => lambda { |file_path, metadata|
48+
metadata[:type].nil? && routing_path_regex =~ file_path
49+
}
50+
51+
view_path_regex = self.escaped_path(%w[spec views])
52+
self.include RSpec::Rails::ViewExampleGroup,
53+
:type => :view,
54+
:file_path => lambda { |file_path, metadata|
55+
metadata[:type].nil? && view_path_regex =~ file_path
56+
}
57+
58+
feature_example_regex = self.escaped_path(%w[spec features])
59+
self.include RSpec::Rails::FeatureExampleGroup,
60+
:type => :feature,
61+
:file_path => lambda { |file_path, metadata|
62+
metadata[:type].nil? && feature_example_regex =~ file_path
63+
}
64+
end
65+
end

spec/spec_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,6 @@ def self.run_all(reporter=nil)
3636
config.after(:each) do
3737
RSpec.instance_variable_set(:@world, real_world)
3838
end
39+
40+
config.infer_spec_type_from_file_location!
3941
end

0 commit comments

Comments
 (0)