Debian Codegraph @

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

raw symbols SCIP
1 RSpec.describe "exist matcher" do
2 it_behaves_like "an RSpec value matcher", :valid_value => Class.new { def exist?; true; end }.new,
3 :invalid_value => Class.new { def exist?; false; end }.new do
4 let(:matcher) { exist }
5 end
6
7 context "when the object does not respond to #exist? or #exists?" do
8 subject { double }
9
10 [:to, :not_to].each do |expect_method|
11 describe "expect(...).#{expect_method} exist" do
12 it "fails" do
13 expect {
14 expect(subject).send(expect_method, exist)
15 }.to fail_including("it does not respond to either `exist?` or `exists?`")
16 end
17 end
18 end
19 end
20
21 it 'composes gracefully' do
22 expect([
23 double,
24 double(:exists? => false),
25 double(:exists? => true),
26 ]).to include existing
27 end
28
29 [:exist?, :exists?].each do |predicate|
30 context "when the object responds to ##{predicate}" do
31 describe "expect(...).to exist" do
32 it "passes if #{predicate}" do
33 expect(double(predicate => true)).to exist
34 end
35
36 it "fails if not #{predicate}" do
37 expect {
38 expect(double(predicate => false)).to exist
39 }.to fail_with(/expected .* to exist/)
40 end
41
42 it 'works when the object overrides `send`' do
43 klass = Struct.new(:message) do
44 def send
45 :message_sent
46 end
47
48 define_method predicate do
49 true
50 end
51 end
52
53 expect(klass.new("msg")).to exist
54 end
55 end
56
57 describe "expect(...).not_to exist" do
58 it "passes if not #{predicate}" do
59 expect(double(predicate => false)).not_to exist
60 end
61
62 it "fails if #{predicate}" do
63 expect {
64 expect(double(predicate => true)).not_to exist
65 }.to fail_with(/expected .* not to exist/)
66 end
67 end
68 end
69 end
70
71 context "when the object responds to #exist? and #exists?" do
72 context "when they both return falsey values" do
73 subject { double(:exist? => false, :exists? => nil) }
74
75 describe "expect(...).not_to exist" do
76 it "passes" do
77 expect(subject).not_to exist
78 end
79 end
80
81 describe "expect(...).to exist" do
82 it "fails" do
83 expect {
84 expect(subject).to exist
85 }.to fail_with(/expected .* to exist/)
86 end
87 end
88 end
89
90 context "when they both return truthy values" do
91 subject { double(:exist? => true, :exists? => "something true") }
92
93 describe "expect(...).not_to exist" do
94 it "fails" do
95 expect {
96 expect(subject).not_to exist
97 }.to fail_with(/expected .* not to exist/)
98 end
99 end
100
101 describe "expect(...).to exist" do
102 it "passes" do
103 expect(subject).to exist
104 end
105 end
106 end
107
108 context "when they return values with different truthiness" do
109 subject { double(:exist? => true, :exists? => false) }
110
111 [:to, :not_to].each do |expect_method|
112 describe "expect(...).#{expect_method} exist" do
113 it "fails" do
114 expect {
115 expect(subject).send(expect_method, exist)
116 }.to fail_including("`exist?` and `exists?` returned different values")
117 end
118 end
119 end
120 end
121
122 context "when one predicate is deprecated" do
123 context 'File has deprecated exists?' do
124 it 'will not call exists? triggering the warning' do
125 expect(File).to exist __FILE__
126 end
127 end
128
129 context 'FileTest has deprecated exists?' do
130 it 'will not call exists? triggering the warning' do
131 expect(FileTest).to exist __FILE__
132 end
133 end
134
135 if RUBY_VERSION > "1.9"
136 context 'Dir has deprecated exists?' do
137 it 'will not call exists? triggering the warning' do
138 expect(Dir).to exist Dir.pwd
139 end
140 end
141 end
142 end
143 end
144
145 it 'passes any provided arguments to the call to #exist?' do
146 object = double
147 expect(object).to receive(:exist?).with(:foo, :bar) { true }.at_least(:once)
148
149 expect(object).to exist(:foo, :bar)
150 end
151
152 it 'memoizes the call to `exist?` because it can be expensive (such as doing a DB query)' do
153 object = double
154 allow(object).to receive(:exist?) { false }
155 expect { expect(object).to exist }.to fail
156
157 expect(object).to have_received(:exist?).once
158 end
159 end