Skip to content

Commit 3ca7323

Browse files
committed
Merge pull request #2353 from rspec/be_true_to_yourself
Change the expectation for predicate methods
1 parent 4e360fb commit 3ca7323

File tree

2 files changed

+54
-16
lines changed

2 files changed

+54
-16
lines changed

lib/rspec/rails/configuration.rb

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# rubocop: disable Metrics/ModuleLength
12
module RSpec
23
module Rails
34
# Fake class to document RSpec Rails configuration options. In practice,
@@ -105,7 +106,35 @@ def render_views
105106
end
106107

107108
def render_views?
108-
rendering_views
109+
rendering_views?
110+
end
111+
112+
def rendering_views?
113+
!!rendering_views
114+
end
115+
116+
# Define boolean predicates rather than relying on rspec-core due
117+
# to the bug fix in rspec/rspec-core#2736, note some of these
118+
# predicates are a bit nonsensical, but they exist for backwards
119+
# compatibility, we can tidy these up in `rspec-rails` 5.
120+
def fixture_path?
121+
!!fixture_path
122+
end
123+
124+
def global_fixtures?
125+
!!global_fixtures
126+
end
127+
128+
def infer_base_class_for_anonymous_controllers?
129+
!!infer_base_class_for_anonymous_controllers
130+
end
131+
132+
def use_instantiated_fixtures?
133+
!!use_instantiated_fixtures
134+
end
135+
136+
def use_transactional_fixtures?
137+
!!use_transactional_fixtures
109138
end
110139

111140
def infer_spec_type_from_file_location!
@@ -156,3 +185,4 @@ def filter_rails_from_backtrace!
156185
initialize_configuration RSpec.configuration
157186
end
158187
end
188+
# rubocop: enable Metrics/ModuleLength

spec/rspec/rails/configuration_spec.rb

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,28 @@
2323
opts ||= {}
2424
default_value = opts[:default]
2525
alias_setting = opts[:alias_with]
26-
query_method = "#{accessor}?".to_sym
26+
predicate_method = "#{accessor}?".to_sym
2727
command_method = "#{accessor}=".to_sym
2828

29-
specify "`##{query_method}` is `#{default_value.inspect}` by default" do
30-
expect(config.send(query_method)).to be(default_value)
29+
specify "`##{accessor}` is `#{default_value.inspect}` by default" do
30+
expect(config.send(accessor)).to eq default_value
31+
end
32+
33+
specify "`##{predicate_method}` is `#{!!default_value}` by default" do
34+
expect(config.send(predicate_method)).to be !!default_value
35+
end
36+
37+
specify "`##{predicate_method}` is `#{!!default_value}` by default" do
38+
expect(config.send(predicate_method)).to be !!default_value
3139
end
3240

3341
describe "`##{command_method}`" do
34-
it "changes `#{query_method}` to the provided value" do
42+
it "changes `#{predicate_method}` to the true for a truthy value" do
43+
config.send(command_method, nil)
44+
expect(config.send(predicate_method)).to be false
3545
expect {
3646
config.send(command_method, :a_value)
37-
}.to change { config.send(query_method) }.to(:a_value)
47+
}.to change { config.send(predicate_method) }.to(true)
3848
end
3949

4050
it "sets `#{accessor}` to the provided value" do
@@ -72,8 +82,8 @@
7282

7383
include_examples "adds setting", :rendering_views
7484

75-
specify "`#render_views?` is falsey by default" do
76-
expect(config.render_views?).to be_falsey
85+
specify "`#render_views?` is false by default" do
86+
expect(config.render_views?).to be false
7787
end
7888

7989
specify "`#render_views` sets `render_views?` to `true`" do
@@ -83,16 +93,14 @@
8393
end
8494

8595
describe "`#render_views=`" do
86-
it "sets `render_views?` to the provided value" do
87-
expect {
88-
config.render_views = false
89-
}.to change { config.render_views? }.from(nil).to(false)
90-
end
91-
92-
it "sets `render_views` to the provided value" do
96+
it "sets `render_views?` to the truthyness of the provided value" do
9397
expect {
9498
config.render_views = :a_value
95-
}.to change { config.render_views? }.to(:a_value)
99+
}.to change { config.render_views? }.from(false).to(true)
100+
# this is repeated to put the value back to false
101+
expect {
102+
config.render_views = false
103+
}.to change { config.render_views? }.from(true).to(false)
96104
end
97105
end
98106
end

0 commit comments

Comments
 (0)