|
1 | 1 | require "spec_helper"
|
| 2 | +require 'rspec/support/spec/in_sub_process' |
| 3 | + |
| 4 | +RSpec.describe "Configuration" do |
| 5 | + |
| 6 | + include RSpec::Support::InSubProcess |
| 7 | + |
| 8 | + subject(:config) { RSpec::Core::Configuration.new } |
2 | 9 |
|
3 |
| -describe "configuration" do |
4 | 10 | before do
|
5 |
| - @orig_render_views = RSpec.configuration.render_views? |
| 11 | + RSpec::Rails.initialize_configuration(config) |
| 12 | + end |
| 13 | + |
| 14 | + it "adds 'vendor/' to the backtrace exclusions" do |
| 15 | + expect(config.backtrace_exclusion_patterns).to include(/vendor\//) |
| 16 | + end |
| 17 | + |
| 18 | + it "adds 'lib/rspec/rails' to the backtrace exclusions" do |
| 19 | + expect( |
| 20 | + config.backtrace_exclusion_patterns |
| 21 | + ).to include(%r{ lib/rspec/rails }) |
| 22 | + end |
| 23 | + |
| 24 | + shared_examples_for "adds setting" do |accessor, opts| |
| 25 | + opts ||= {} |
| 26 | + default_value = opts[:default] |
| 27 | + alias_setting = opts[:alias_with] |
| 28 | + query_method = "#{accessor}?".to_sym |
| 29 | + command_method = "#{accessor}=".to_sym |
| 30 | + |
| 31 | + specify "`##{query_method}` is `#{default_value.inspect}` by default" do |
| 32 | + expect(config.send(query_method)).to be(default_value) |
| 33 | + end |
| 34 | + |
| 35 | + describe "`##{command_method}`" do |
| 36 | + it "changes `#{query_method}` to the provided value" do |
| 37 | + expect { |
| 38 | + config.send(command_method, :a_value) |
| 39 | + }.to change { config.send(query_method) }.to(:a_value) |
| 40 | + end |
| 41 | + |
| 42 | + it "sets `#{accessor}` to the provided value" do |
| 43 | + expect { |
| 44 | + config.send(command_method, :a_value) |
| 45 | + }.to change { |
| 46 | + config.send(accessor) |
| 47 | + }.from(default_value).to(:a_value) |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + if alias_setting |
| 52 | + specify "`##{alias_setting}` is an alias for `#{accessor}`" do |
| 53 | + expect { |
| 54 | + config.send(command_method, :a_value) |
| 55 | + }.to change { config.send(alias_setting) }.to(:a_value) |
| 56 | + end |
| 57 | + end |
6 | 58 | end
|
7 | 59 |
|
8 |
| - after do |
9 |
| - RSpec.configuration.render_views = @orig_render_views |
| 60 | + context "adds settings" do |
| 61 | + include_examples "adds setting", |
| 62 | + :infer_base_class_for_anonymous_controllers, |
| 63 | + :default => true |
| 64 | + |
| 65 | + include_examples "adds setting", |
| 66 | + :use_transactional_fixtures, |
| 67 | + :alias_with => :use_transactional_examples |
| 68 | + |
| 69 | + include_examples "adds setting", :use_instantiated_fixtures |
| 70 | + |
| 71 | + include_examples "adds setting", :global_fixtures |
| 72 | + |
| 73 | + include_examples "adds setting", :fixture_path |
| 74 | + |
| 75 | + include_examples "adds setting", :rendering_views |
| 76 | + |
| 77 | + specify "`#render_views?` is falsey by default" do |
| 78 | + expect(config.render_views?).to be_falsey |
| 79 | + end |
| 80 | + |
| 81 | + specify "`#render_views` sets `render_views?` to `true`" do |
| 82 | + expect { |
| 83 | + config.render_views |
| 84 | + }.to change { config.render_views? }.to be(true) |
| 85 | + end |
| 86 | + |
| 87 | + describe "`#render_views=`" do |
| 88 | + it "sets `render_views?` to the provided value" do |
| 89 | + expect { |
| 90 | + config.render_views = false |
| 91 | + }.to change { config.render_views? }.from(nil).to(false) |
| 92 | + end |
| 93 | + |
| 94 | + it "sets `render_views` to the provided value" do |
| 95 | + expect { |
| 96 | + config.render_views = :a_value |
| 97 | + }.to change { config.render_views? }.to(:a_value) |
| 98 | + end |
| 99 | + end |
| 100 | + end |
| 101 | + |
| 102 | + describe "#infer_spec_type_from_file_location!" do |
| 103 | + def in_inferring_type_from_location_environment |
| 104 | + in_sub_process do |
| 105 | + RSpec.configuration.infer_spec_type_from_file_location! |
| 106 | + yield |
| 107 | + end |
| 108 | + end |
| 109 | + |
| 110 | + shared_examples_for "infers type from location" do |type, location| |
| 111 | + it "sets the type to `#{type.inspect}` for file path `#{location}`" do |
| 112 | + in_inferring_type_from_location_environment do |
| 113 | + allow(RSpec::Core::Metadata).to receive(:relative_path).and_return( |
| 114 | + "./#{location}/foos_spec.rb" |
| 115 | + ) |
| 116 | + group = RSpec.describe("Arbitrary Description") |
| 117 | + expect(group.metadata).to include(:type => type) |
| 118 | + end |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + include_examples "infers type from location", |
| 123 | + :controller, |
| 124 | + "spec/controllers" |
| 125 | + include_examples "infers type from location", :helper, "spec/helpers" |
| 126 | + include_examples "infers type from location", :mailer, "spec/mailers" |
| 127 | + include_examples "infers type from location", :model, "spec/models" |
| 128 | + include_examples "infers type from location", :request, "spec/requests" |
| 129 | + include_examples "infers type from location", :request, "spec/integration" |
| 130 | + include_examples "infers type from location", :request, "spec/api" |
| 131 | + include_examples "infers type from location", :routing, "spec/routing" |
| 132 | + include_examples "infers type from location", :view, "spec/views" |
| 133 | + include_examples "infers type from location", :feature, "spec/features" |
10 | 134 | end
|
11 | 135 |
|
12 |
| - describe "#render_views?" do |
13 |
| - it "is false by default" do |
14 |
| - expect(RSpec.configuration.render_views?).to be_falsey |
| 136 | + it "fixture support is included with metadata `:use_fixtures`" do |
| 137 | + in_sub_process do |
| 138 | + RSpec.configuration.global_fixtures = [:foo] |
| 139 | + RSpec.configuration.fixture_path = "custom/path" |
| 140 | + |
| 141 | + group = RSpec.describe("Arbitrary Description", :use_fixtures) |
| 142 | + |
| 143 | + expect(group).to respond_to(:fixture_path) |
| 144 | + expect(group.fixture_path).to eq("custom/path") |
| 145 | + expect(group.new.respond_to?(:foo, true)).to be(true) |
15 | 146 | end
|
16 | 147 | end
|
17 | 148 |
|
18 |
| - describe "#render_views" do |
19 |
| - it "sets render_views? to return true" do |
20 |
| - RSpec.configuration.render_views = false |
21 |
| - RSpec.configuration.render_views |
| 149 | + it "metadata `:type => :controller` sets up controller example groups" do |
| 150 | + a_controller_class = Class.new |
| 151 | + stub_const "SomeController", a_controller_class |
| 152 | + |
| 153 | + group = RSpec.describe(SomeController, :type => :controller) |
| 154 | + |
| 155 | + expect(group.controller_class).to be(a_controller_class) |
| 156 | + expect(group.new).to be_a(RSpec::Rails::ControllerExampleGroup) |
| 157 | + end |
| 158 | + |
| 159 | + it "metadata `:type => :helper` sets up helper example groups" do |
| 160 | + a_helper_module = Module.new |
| 161 | + stub_const "SomeHelper", a_helper_module |
| 162 | + |
| 163 | + group = RSpec.describe(SomeHelper, :type => :helper) |
| 164 | + |
| 165 | + expect( |
| 166 | + group.determine_default_helper_class(:ignored) |
| 167 | + ).to be(a_helper_module) |
| 168 | + expect(group.new).to be_a(RSpec::Rails::HelperExampleGroup) |
| 169 | + end |
| 170 | + |
| 171 | + it "metadata `:type => :model` sets up model example groups" do |
| 172 | + a_model_class = Class.new |
| 173 | + stub_const "SomeModel", a_model_class |
| 174 | + |
| 175 | + group = RSpec.describe(SomeModel, :type => :model) |
| 176 | + |
| 177 | + expect(group.new).to be_a(RSpec::Rails::ModelExampleGroup) |
| 178 | + end |
| 179 | + |
| 180 | + it "metadata `:type => :request` sets up request example groups" do |
| 181 | + a_rails_app = double("Rails application") |
| 182 | + the_rails_module = double("Rails module", :application => a_rails_app) |
| 183 | + stub_const "Rails", the_rails_module |
22 | 184 |
|
23 |
| - expect(RSpec.configuration.render_views?).to be_truthy |
| 185 | + group = RSpec.describe("Some Request API", :type => :request) |
| 186 | + |
| 187 | + expect(group.new.app).to be(a_rails_app) |
| 188 | + expect(group.new).to be_a(RSpec::Rails::RequestExampleGroup) |
| 189 | + end |
| 190 | + |
| 191 | + it "metadata `:type => :routing` sets up routing example groups" do |
| 192 | + a_controller_class = Class.new |
| 193 | + stub_const "SomeController", a_controller_class |
| 194 | + |
| 195 | + group = RSpec.describe(SomeController, :type => :routing) |
| 196 | + |
| 197 | + expect(group).to respond_to(:routes) |
| 198 | + expect(group.new).to be_a(RSpec::Rails::RoutingExampleGroup) |
| 199 | + end |
| 200 | + |
| 201 | + it "metadata `:type => :view` sets up view example groups" do |
| 202 | + a_helper_module = Module.new |
| 203 | + stub_const "SomeControllerHelper", a_helper_module |
| 204 | + |
| 205 | + group = RSpec.describe("some_controller/action.html.erb", :type => :view) |
| 206 | + |
| 207 | + expect(group._default_helper).to be(a_helper_module) |
| 208 | + expect(group.new).to be_a(RSpec::Rails::ViewExampleGroup) |
| 209 | + end |
| 210 | + |
| 211 | + it "metadata `:type => :feature` sets up feature example groups" do |
| 212 | + a_rails_app = double("Rails application") |
| 213 | + the_rails_module = double("Rails module", :application => a_rails_app) |
| 214 | + stub_const "Rails", the_rails_module |
| 215 | + |
| 216 | + group = RSpec.describe("Some feature description", :type => :feature) |
| 217 | + example = group.new |
| 218 | + |
| 219 | + expect(example).to respond_to(:visit) |
| 220 | + expect(example).to be_a(RSpec::Rails::FeatureExampleGroup) |
| 221 | + end |
| 222 | + |
| 223 | + if defined?(ActionMailer) |
| 224 | + it "metadata `:type => :mailer` sets up mailer example groups" do |
| 225 | + a_mailer_class = Class.new |
| 226 | + stub_const "SomeMailer", a_mailer_class |
| 227 | + group = RSpec.describe(SomeMailer, :type => :mailer) |
| 228 | + expect(group.mailer_class).to be(a_mailer_class) |
| 229 | + expect(group.new).to be_a(RSpec::Rails::MailerExampleGroup) |
24 | 230 | end
|
25 | 231 | end
|
| 232 | + |
26 | 233 | end
|
0 commit comments