Debian Codegraph @

rspec-expectations/spec/rspec/matchers/built_in/has_spec.rb

raw symbols SCIP
1 RSpec.describe "expect(...).to have_sym(*args)" do
2 it_behaves_like "an RSpec value matcher", :valid_value => { :a => 1 },
3 :invalid_value => {} do
4 let(:matcher) { have_key(:a) }
5 end
6
7 it "passes if #has_sym?(*args) returns true" do
8 expect({ :a => "A" }).to have_key(:a)
9 end
10
11 if RSpec::Support::RubyFeatures.required_kw_args_supported?
12 binding.eval(<<-CODE, __FILE__, __LINE__)
13 it 'supports the use of required keyword arguments' do
14 thing = Class.new { def has_keyword?(keyword:); keyword == 'a'; end }
15 expect(thing.new).to have_keyword(keyword: 'a')
16 end
17 CODE
18 end
19
20 if RSpec::Support::RubyFeatures.kw_args_supported?
21 binding.eval(<<-CODE, __FILE__, __LINE__)
22 it 'supports the use of optional keyword arguments' do
23 thing = Class.new { def has_keyword?(keyword: 'b'); keyword == 'a'; end }
24 expect(thing.new).to have_keyword(keyword: 'a')
25 end
26 CODE
27 end
28
29 it "fails if #has_sym?(*args) returns false" do
30 expect {
31 expect({ :b => "B" }).to have_key(:a)
32 }.to fail_with("expected `#{{ :b=>"B" }.inspect}.has_key?(:a)` to return true, got false")
33 end
34
35 obj_with_block_method = Object.new
36 def obj_with_block_method.has_some_stuff?; yield; end
37
38 it 'forwards the given `{ }` block on to the `has_xyz?` method' do
39 expect(obj_with_block_method).to have_some_stuff { true }
40 expect(obj_with_block_method).to_not have_some_stuff { false }
41 end
42
43 it 'forwards the given `do..end` block on to the `has_xyz?` method' do
44 expect(obj_with_block_method).to have_some_stuff do
45 true
46 end
47
48 expect(obj_with_block_method).to_not have_some_stuff do
49 false
50 end
51 end
52
53 it 'favors a curly brace block over a do...end one since it binds to the matcher method' do
54 expect(obj_with_block_method).to have_some_stuff { true } do
55 false
56 end
57
58 expect(obj_with_block_method).not_to have_some_stuff { false } do
59 true
60 end
61 end
62
63 it 'does not include any args in the failure message if no args were given to the matcher' do
64 o = Object.new
65 def o.has_some_stuff?; false; end
66 expect {
67 expect(o).to have_some_stuff
68 }.to fail_with("expected `#{o.inspect}.has_some_stuff?` to return true, got false")
69 end
70
71 it 'includes multiple args in the failure message if multiple args were given to the matcher' do
72 o = Object.new
73 def o.has_some_stuff?(*_); false; end
74 expect {
75 expect(o).to have_some_stuff(:a, 7, "foo")
76 }.to fail_with(%Q{expected `#{o.inspect}.has_some_stuff?(:a, 7, "foo")` to return true, got false})
77 end
78
79 it "fails if #has_sym?(*args) returns nil" do
80 klass = Class.new do
81 def has_foo?
82 end
83 end
84 expect {
85 expect(klass.new).to have_foo
86 }.to fail_with(/expected `.*\.has_foo\?` to return true, got nil/)
87 end
88
89 it 'fails if #has_sym?(*args) is private' do
90 klass = Class.new do
91 private
92 def has_foo?
93 true
94 end
95 end
96 expect { expect(klass.new).to have_foo }.to fail_with(/private/)
97 end
98
99 it "fails if target does not respond to #has_sym?" do
100 expect {
101 expect(Object.new).to have_key(:a)
102 }.to fail_including('to respond to `has_key?`')
103 end
104
105 it "reraises an exception thrown in #has_sym?(*args)" do
106 o = Object.new
107 def o.has_sym?(*_args)
108 raise "Funky exception"
109 end
110 expect {
111 expect(o).to have_sym(:foo)
112 }.to raise_error("Funky exception")
113 end
114
115 it 'allows composable aliases to be defined' do
116 RSpec::Matchers.alias_matcher :an_object_having_sym, :have_sym
117 o = Object.new
118 def o.has_sym?(sym); sym == :foo; end
119
120 expect(o).to an_object_having_sym(:foo)
121 expect(o).not_to an_object_having_sym(:bar)
122
123 expect(an_object_having_sym(:foo).description).to eq("an object having sym :foo")
124 end
125
126 it 'composes gracefully' do
127 RSpec::Matchers.alias_matcher :an_object_having_foo, :have_foo
128
129 expect([
130 double,
131 double(:has_foo? => false),
132 double(:has_foo? => true),
133 ]).to include an_object_having_foo
134 end
135 end
136
137 RSpec.describe "expect(...).not_to have_sym(*args)" do
138 it "passes if #has_sym?(*args) returns false" do
139 expect({ :a => "A" }).not_to have_key(:b)
140 end
141
142 context "when strict_predicate_matchers is set to true" do
143 it "fails when #has_sym? returns nil" do
144 actual = double("actual", :has_foo? => nil)
145 expect {
146 expect(actual).not_to have_foo
147 }.to fail_with("expected `#{actual.inspect}.has_foo?` to return false, got nil")
148 end
149 end
150
151 context "when strict_predicate_matchers is set to false" do
152 around do |example|
153 RSpec::Expectations.configuration.strict_predicate_matchers = false
154 example.run
155 RSpec::Expectations.configuration.strict_predicate_matchers = true
156 end
157
158 it "passes if #has_sym?(*args) returns nil" do
159 actual = double("actual", :has_foo? => nil)
160 expect(actual).not_to have_foo
161 end
162
163 it "allows dynamic matchers to pass for supplied methods on spies" do
164 thing = spy("thing", :furg? => true)
165 expect(thing).to be_furg(:foo)
166 expect(thing).to have_received(:furg?).with(:foo)
167 end
168
169 it "does not allow dynamic matchers to pass for inferred methods on spies" do
170 thing = spy("thing")
171 expect {
172 expect(thing).to be_furg(:foo)
173 }.to fail
174 expect(thing).not_to have_received(:furg?)
175 end
176 end
177
178 it "fails if #has_sym?(*args) returns true" do
179 expect {
180 expect({ :a => "A" }).not_to have_key(:a)
181 }.to fail_with("expected `#{{ :a=>"A" }.inspect}.has_key?(:a)` to return false, got true")
182 end
183
184 it "fails if target does not respond to #has_sym?" do
185 expect {
186 expect(Object.new).not_to have_key(:a)
187 }.to fail_including('to respond to `has_key?`')
188 end
189
190 it "reraises an exception thrown in #has_sym?(*args)" do
191 o = Object.new
192 def o.has_sym?(*_args)
193 raise "Funky exception"
194 end
195 expect {
196 expect(o).not_to have_sym(:foo)
197 }.to raise_error("Funky exception")
198 end
199
200 it 'does not include any args in the failure message if no args were given to the matcher' do
201 o = Object.new
202 def o.has_some_stuff?; true; end
203 expect {
204 expect(o).not_to have_some_stuff
205 }.to fail_with("expected `#{o.inspect}.has_some_stuff?` to return false, got true")
206 end
207
208 it 'includes multiple args in the failure message if multiple args were given to the matcher' do
209 o = Object.new
210 def o.has_some_stuff?(*_); true; end
211 expect {
212 expect(o).not_to have_some_stuff(:a, 7, "foo")
213 }.to fail_with(%Q{expected `#{o.inspect}.has_some_stuff?(:a, 7, "foo")` to return false, got true})
214 end
215 end
216
217 RSpec.describe "has" do
218 it "works when the target implements #send" do
219 o = { :a => "A" }
220 def o.send(*_args); raise "DOH! Library developers shouldn't use #send!" end
221 expect {
222 expect(o).to have_key(:a)
223 }.not_to raise_error
224 end
225 end