|
1 | | -RSpec::configure do |c| |
| 1 | +module RSpec |
| 2 | + module Rails |
| 3 | + # @private |
| 4 | + def self.add_rspec_rails_config_api_to(config) |
| 5 | + # controller settings |
| 6 | + config.add_setting :infer_base_class_for_anonymous_controllers, :default => true |
| 7 | + |
| 8 | + # fixture support |
| 9 | + config.include RSpec::Rails::FixtureSupport |
| 10 | + config.add_setting :use_transactional_fixtures, :alias_with => :use_transactional_examples |
| 11 | + config.add_setting :use_instantiated_fixtures |
| 12 | + config.add_setting :global_fixtures |
| 13 | + config.add_setting :fixture_path |
| 14 | + |
| 15 | + # view rendering settings |
| 16 | + # This allows us to expose `render_views` as a config option even though it |
| 17 | + # breaks the convention of other options by using `render_views` as a |
| 18 | + # command (i.e. render_views = true), where it would normally be used as a |
| 19 | + # getter. This makes it easier for rspec-rails users because we use |
| 20 | + # `render_views` directly in example groups, so this aligns the two APIs, |
| 21 | + # but requires this workaround: |
| 22 | + config.add_setting :rendering_views, :default => false |
| 23 | + |
| 24 | + def config.render_views=(val) |
| 25 | + self.rendering_views = val |
| 26 | + end |
| 27 | + |
| 28 | + def config.render_views |
| 29 | + self.rendering_views = true |
| 30 | + end |
| 31 | + |
| 32 | + def config.render_views? |
| 33 | + rendering_views |
| 34 | + end |
| 35 | + |
| 36 | + def config.infer_spec_type_from_file_location! |
| 37 | + def self.escaped_path(*parts) |
| 38 | + Regexp.compile(parts.join('[\\\/]') + '[\\\/]') |
| 39 | + end |
| 40 | + |
| 41 | + controller_path_regex = self.escaped_path(%w[spec controllers]) |
| 42 | + self.include RSpec::Rails::ControllerExampleGroup, |
| 43 | + :type => :controller, |
| 44 | + :file_path => lambda { |file_path, metadata| |
| 45 | + metadata[:type].nil? && controller_path_regex =~ file_path |
| 46 | + } |
| 47 | + |
| 48 | + helper_path_regex = self.escaped_path(%w[spec helpers]) |
| 49 | + self.include RSpec::Rails::HelperExampleGroup, |
| 50 | + :type => :helper, |
| 51 | + :file_path => lambda { |file_path, metadata| |
| 52 | + metadata[:type].nil? && helper_path_regex =~ file_path |
| 53 | + } |
| 54 | + |
| 55 | + mailer_path_regex = self.escaped_path(%w[spec mailers]) |
| 56 | + if defined?(RSpec::Rails::MailerExampleGroup) |
| 57 | + self.include RSpec::Rails::MailerExampleGroup, |
| 58 | + :type => :mailer, |
| 59 | + :file_path => lambda { |file_path, metadata| |
| 60 | + metadata[:type].nil? && mailer_path_regex =~ file_path |
| 61 | + } |
| 62 | + end |
| 63 | + |
| 64 | + model_path_regex = self.escaped_path(%w[spec models]) |
| 65 | + self.include RSpec::Rails::ModelExampleGroup, |
| 66 | + :type => :model, |
| 67 | + :file_path => lambda { |file_path, metadata| |
| 68 | + metadata[:type].nil? && model_path_regex =~ file_path |
| 69 | + } |
| 70 | + |
| 71 | + request_path_regex = self.escaped_path(%w[spec (requests|integration|api)]) |
| 72 | + self.include RSpec::Rails::RequestExampleGroup, |
| 73 | + :type => :request, |
| 74 | + :file_path => lambda { |file_path, metadata| |
| 75 | + metadata[:type].nil? && request_path_regex =~ file_path |
| 76 | + } |
| 77 | + |
| 78 | + routing_path_regex = self.escaped_path(%w[spec routing]) |
| 79 | + self.include RSpec::Rails::RoutingExampleGroup, |
| 80 | + :type => :routing, |
| 81 | + :file_path => lambda { |file_path, metadata| |
| 82 | + metadata[:type].nil? && routing_path_regex =~ file_path |
| 83 | + } |
| 84 | + |
| 85 | + view_path_regex = self.escaped_path(%w[spec views]) |
| 86 | + self.include RSpec::Rails::ViewExampleGroup, |
| 87 | + :type => :view, |
| 88 | + :file_path => lambda { |file_path, metadata| |
| 89 | + metadata[:type].nil? && view_path_regex =~ file_path |
| 90 | + } |
| 91 | + |
| 92 | + feature_example_regex = self.escaped_path(%w[spec features]) |
| 93 | + self.include RSpec::Rails::FeatureExampleGroup, |
| 94 | + :type => :feature, |
| 95 | + :file_path => lambda { |file_path, metadata| |
| 96 | + metadata[:type].nil? && feature_example_regex =~ file_path |
| 97 | + } |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | +end |
| 102 | + |
| 103 | +RSpec.configure do |c| |
| 104 | + RSpec::Rails.add_rspec_rails_config_api_to(c) |
| 105 | + |
| 106 | + c.backtrace_exclusion_patterns << /vendor\// |
| 107 | + c.backtrace_exclusion_patterns << /lib\/rspec\/rails/ |
| 108 | + |
2 | 109 | def c.escaped_path(*parts) |
3 | 110 | Regexp.compile(parts.join('[\\\/]') + '[\\\/]') |
4 | 111 | end |
|
0 commit comments