rspec-core 3.0.0.beta2 → 3.0.0.rc1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (201) hide show
  1. checksums.yaml +7 -0
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/.yardopts +1 -0
  5. data/Changelog.md +297 -57
  6. data/README.md +16 -13
  7. data/lib/rspec/core.rb +55 -84
  8. data/lib/rspec/core/backport_random.rb +35 -3
  9. data/lib/rspec/core/backtrace_formatter.rb +4 -13
  10. data/lib/rspec/core/configuration.rb +330 -114
  11. data/lib/rspec/core/configuration_options.rb +38 -22
  12. data/lib/rspec/core/drb.rb +111 -0
  13. data/lib/rspec/core/dsl.rb +8 -2
  14. data/lib/rspec/core/example.rb +203 -94
  15. data/lib/rspec/core/example_group.rb +344 -316
  16. data/lib/rspec/core/filter_manager.rb +135 -90
  17. data/lib/rspec/core/flat_map.rb +1 -0
  18. data/lib/rspec/core/formatters.rb +50 -14
  19. data/lib/rspec/core/formatters/base_formatter.rb +32 -138
  20. data/lib/rspec/core/formatters/base_text_formatter.rb +32 -253
  21. data/lib/rspec/core/formatters/console_codes.rb +65 -0
  22. data/lib/rspec/core/formatters/deprecation_formatter.rb +24 -15
  23. data/lib/rspec/core/formatters/documentation_formatter.rb +7 -10
  24. data/lib/rspec/core/formatters/helpers.rb +15 -9
  25. data/lib/rspec/core/formatters/html_formatter.rb +17 -16
  26. data/lib/rspec/core/formatters/html_printer.rb +1 -0
  27. data/lib/rspec/core/formatters/json_formatter.rb +18 -20
  28. data/lib/rspec/core/formatters/profile_formatter.rb +67 -0
  29. data/lib/rspec/core/formatters/progress_formatter.rb +6 -7
  30. data/lib/rspec/core/formatters/snippet_extractor.rb +8 -6
  31. data/lib/rspec/core/hooks.rb +131 -125
  32. data/lib/rspec/core/memoized_helpers.rb +31 -26
  33. data/lib/rspec/core/metadata.rb +277 -184
  34. data/lib/rspec/core/metadata_filter.rb +86 -0
  35. data/lib/rspec/core/minitest_assertions_adapter.rb +28 -0
  36. data/lib/rspec/core/mocking_adapters/flexmock.rb +1 -1
  37. data/lib/rspec/core/mocking_adapters/mocha.rb +1 -1
  38. data/lib/rspec/core/mocking_adapters/null.rb +1 -1
  39. data/lib/rspec/core/mocking_adapters/rr.rb +2 -1
  40. data/lib/rspec/core/mocking_adapters/rspec.rb +1 -1
  41. data/lib/rspec/core/notifications.rb +435 -24
  42. data/lib/rspec/core/option_parser.rb +16 -25
  43. data/lib/rspec/core/ordering.rb +3 -1
  44. data/lib/rspec/core/pending.rb +57 -33
  45. data/lib/rspec/core/project_initializer.rb +2 -0
  46. data/lib/rspec/core/project_initializer/spec_helper.rb +5 -4
  47. data/lib/rspec/core/rake_task.rb +45 -20
  48. data/lib/rspec/core/reporter.rb +50 -22
  49. data/lib/rspec/core/ruby_project.rb +1 -0
  50. data/lib/rspec/core/runner.rb +93 -39
  51. data/lib/rspec/core/shared_context.rb +7 -5
  52. data/lib/rspec/core/shared_example_group.rb +85 -77
  53. data/lib/rspec/core/test_unit_assertions_adapter.rb +30 -0
  54. data/lib/rspec/core/version.rb +3 -1
  55. data/lib/rspec/core/warnings.rb +35 -17
  56. data/lib/rspec/core/world.rb +57 -5
  57. metadata +56 -369
  58. metadata.gz.sig +3 -3
  59. data/features/README.md +0 -13
  60. data/features/Upgrade.md +0 -352
  61. data/features/command_line/README.md +0 -25
  62. data/features/command_line/dry_run.feature +0 -29
  63. data/features/command_line/example_name_option.feature +0 -97
  64. data/features/command_line/exit_status.feature +0 -82
  65. data/features/command_line/fail_fast.feature +0 -26
  66. data/features/command_line/format_option.feature +0 -75
  67. data/features/command_line/init.feature +0 -57
  68. data/features/command_line/line_number_appended_to_path.feature +0 -140
  69. data/features/command_line/line_number_option.feature +0 -58
  70. data/features/command_line/order.feature +0 -25
  71. data/features/command_line/pattern_option.feature +0 -49
  72. data/features/command_line/rake_task.feature +0 -122
  73. data/features/command_line/randomization.feature +0 -63
  74. data/features/command_line/require_option.feature +0 -43
  75. data/features/command_line/ruby.feature +0 -23
  76. data/features/command_line/tag.feature +0 -98
  77. data/features/command_line/warnings_option.feature +0 -29
  78. data/features/configuration/alias_example_to.feature +0 -39
  79. data/features/configuration/backtrace_exclusion_patterns.feature +0 -105
  80. data/features/configuration/custom_settings.feature +0 -84
  81. data/features/configuration/default_path.feature +0 -38
  82. data/features/configuration/deprecation_stream.feature +0 -58
  83. data/features/configuration/enable_global_dsl.feature +0 -54
  84. data/features/configuration/fail_fast.feature +0 -77
  85. data/features/configuration/failure_exit_code.feature +0 -36
  86. data/features/configuration/order_and_seed.feature +0 -3
  87. data/features/configuration/output_stream.feature +0 -24
  88. data/features/configuration/overriding_global_ordering.feature +0 -93
  89. data/features/configuration/pattern.feature +0 -38
  90. data/features/configuration/profile.feature +0 -220
  91. data/features/configuration/read_options_from_file.feature +0 -90
  92. data/features/configuration/run_all_when_everything_filtered.feature +0 -76
  93. data/features/example_groups/aliasing.feature +0 -48
  94. data/features/example_groups/basic_structure.feature +0 -55
  95. data/features/example_groups/shared_context.feature +0 -74
  96. data/features/example_groups/shared_examples.feature +0 -286
  97. data/features/expectation_framework_integration/configure_expectation_framework.feature +0 -102
  98. data/features/filtering/exclusion_filters.feature +0 -135
  99. data/features/filtering/if_and_unless.feature +0 -138
  100. data/features/filtering/inclusion_filters.feature +0 -101
  101. data/features/formatters/configurable_colors.feature +0 -31
  102. data/features/formatters/custom_formatter.feature +0 -68
  103. data/features/formatters/json_formatter.feature +0 -30
  104. data/features/formatters/regression_tests.feature +0 -95
  105. data/features/formatters/text_formatter.feature +0 -46
  106. data/features/helper_methods/arbitrary_methods.feature +0 -40
  107. data/features/helper_methods/let.feature +0 -50
  108. data/features/helper_methods/modules.feature +0 -146
  109. data/features/hooks/around_hooks.feature +0 -344
  110. data/features/hooks/before_and_after_hooks.feature +0 -427
  111. data/features/hooks/filtering.feature +0 -232
  112. data/features/metadata/current_example.feature +0 -56
  113. data/features/metadata/described_class.feature +0 -17
  114. data/features/metadata/user_defined.feature +0 -100
  115. data/features/mock_framework_integration/use_any_framework.feature +0 -106
  116. data/features/mock_framework_integration/use_flexmock.feature +0 -94
  117. data/features/mock_framework_integration/use_mocha.feature +0 -95
  118. data/features/mock_framework_integration/use_rr.feature +0 -96
  119. data/features/mock_framework_integration/use_rspec.feature +0 -95
  120. data/features/pending_and_skipped_examples/README.md +0 -3
  121. data/features/pending_and_skipped_examples/pending_examples.feature +0 -118
  122. data/features/pending_and_skipped_examples/skipped_examples.feature +0 -106
  123. data/features/spec_files/arbitrary_file_suffix.feature +0 -13
  124. data/features/step_definitions/additional_cli_steps.rb +0 -83
  125. data/features/subject/explicit_subject.feature +0 -101
  126. data/features/subject/implicit_subject.feature +0 -63
  127. data/features/subject/one_liner_syntax.feature +0 -71
  128. data/features/support/env.rb +0 -21
  129. data/features/support/require_expect_syntax_in_aruba_specs.rb +0 -16
  130. data/features/support/rubinius.rb +0 -6
  131. data/lib/rspec/core/command_line.rb +0 -35
  132. data/lib/rspec/core/drb_command_line.rb +0 -26
  133. data/lib/rspec/core/drb_options.rb +0 -87
  134. data/lib/rspec/core/formatters/legacy_formatter.rb +0 -227
  135. data/lib/rspec/core/shared_example_group/collection.rb +0 -27
  136. data/spec/command_line/order_spec.rb +0 -211
  137. data/spec/rspec/core/backtrace_formatter_spec.rb +0 -230
  138. data/spec/rspec/core/command_line_spec.rb +0 -112
  139. data/spec/rspec/core/command_line_spec_output.txt +0 -0
  140. data/spec/rspec/core/configuration_options_spec.rb +0 -409
  141. data/spec/rspec/core/configuration_spec.rb +0 -1479
  142. data/spec/rspec/core/drb_command_line_spec.rb +0 -102
  143. data/spec/rspec/core/drb_options_spec.rb +0 -193
  144. data/spec/rspec/core/dsl_spec.rb +0 -88
  145. data/spec/rspec/core/example_group_spec.rb +0 -1533
  146. data/spec/rspec/core/example_spec.rb +0 -642
  147. data/spec/rspec/core/filter_manager_spec.rb +0 -229
  148. data/spec/rspec/core/formatters/base_formatter_spec.rb +0 -64
  149. data/spec/rspec/core/formatters/base_text_formatter_spec.rb +0 -303
  150. data/spec/rspec/core/formatters/deprecation_formatter_spec.rb +0 -208
  151. data/spec/rspec/core/formatters/documentation_formatter_spec.rb +0 -75
  152. data/spec/rspec/core/formatters/helpers_spec.rb +0 -104
  153. data/spec/rspec/core/formatters/html_formatted-2.1.0.html +0 -392
  154. data/spec/rspec/core/formatters/html_formatted.html +0 -397
  155. data/spec/rspec/core/formatters/html_formatter_spec.rb +0 -122
  156. data/spec/rspec/core/formatters/json_formatter_spec.rb +0 -206
  157. data/spec/rspec/core/formatters/legacy_formatter_spec.rb +0 -137
  158. data/spec/rspec/core/formatters/progress_formatter_spec.rb +0 -43
  159. data/spec/rspec/core/formatters/snippet_extractor_spec.rb +0 -26
  160. data/spec/rspec/core/formatters_spec.rb +0 -120
  161. data/spec/rspec/core/hooks_filtering_spec.rb +0 -227
  162. data/spec/rspec/core/hooks_spec.rb +0 -294
  163. data/spec/rspec/core/memoized_helpers_spec.rb +0 -495
  164. data/spec/rspec/core/metadata_spec.rb +0 -491
  165. data/spec/rspec/core/option_parser_spec.rb +0 -262
  166. data/spec/rspec/core/ordering_spec.rb +0 -102
  167. data/spec/rspec/core/pending_example_spec.rb +0 -117
  168. data/spec/rspec/core/pending_spec.rb +0 -8
  169. data/spec/rspec/core/project_initializer_spec.rb +0 -73
  170. data/spec/rspec/core/rake_task_spec.rb +0 -146
  171. data/spec/rspec/core/random_spec.rb +0 -47
  172. data/spec/rspec/core/reporter_spec.rb +0 -155
  173. data/spec/rspec/core/resources/a_bar.rb +0 -0
  174. data/spec/rspec/core/resources/a_foo.rb +0 -0
  175. data/spec/rspec/core/resources/a_spec.rb +0 -1
  176. data/spec/rspec/core/resources/custom_example_group_runner.rb +0 -14
  177. data/spec/rspec/core/resources/formatter_specs.rb +0 -58
  178. data/spec/rspec/core/resources/utf8_encoded.rb +0 -8
  179. data/spec/rspec/core/rspec_matchers_spec.rb +0 -45
  180. data/spec/rspec/core/ruby_project_spec.rb +0 -26
  181. data/spec/rspec/core/runner_spec.rb +0 -151
  182. data/spec/rspec/core/shared_context_spec.rb +0 -102
  183. data/spec/rspec/core/shared_example_group/collection_spec.rb +0 -57
  184. data/spec/rspec/core/shared_example_group_spec.rb +0 -114
  185. data/spec/rspec/core/warnings_spec.rb +0 -29
  186. data/spec/rspec/core/world_spec.rb +0 -142
  187. data/spec/rspec/core_spec.rb +0 -91
  188. data/spec/spec_helper.rb +0 -160
  189. data/spec/support/config_options_helper.rb +0 -13
  190. data/spec/support/formatter_support.rb +0 -83
  191. data/spec/support/helper_methods.rb +0 -26
  192. data/spec/support/isolate_load_path_mutation.rb +0 -5
  193. data/spec/support/isolated_directory.rb +0 -10
  194. data/spec/support/isolated_home_directory.rb +0 -16
  195. data/spec/support/legacy_formatter_using_sub_classing_example.rb +0 -87
  196. data/spec/support/matchers.rb +0 -85
  197. data/spec/support/mathn_integration_support.rb +0 -12
  198. data/spec/support/old_style_formatter_example.rb +0 -69
  199. data/spec/support/shared_example_groups.rb +0 -13
  200. data/spec/support/spec_files.rb +0 -44
  201. data/spec/support/stderr_splitter.rb +0 -36
@@ -1,495 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RSpec::Core
4
- RSpec.describe MemoizedHelpers do
5
- before(:each) { RSpec.configuration.configure_expectation_framework }
6
-
7
- def subject_value_for(describe_arg, &block)
8
- group = ExampleGroup.describe(describe_arg, &block)
9
- subject_value = nil
10
- group.example { subject_value = subject }
11
- group.run
12
- subject_value
13
- end
14
-
15
- describe "implicit subject" do
16
- describe "with a class" do
17
- it "returns an instance of the class" do
18
- expect(subject_value_for(Array)).to eq([])
19
- end
20
- end
21
-
22
- describe "with a Module" do
23
- it "returns the Module" do
24
- expect(subject_value_for(Enumerable)).to eq(Enumerable)
25
- end
26
- end
27
-
28
- describe "with a string" do
29
- it "returns the string" do
30
- expect(subject_value_for("Foo")).to eq("Foo")
31
- end
32
- end
33
-
34
- describe "with a number" do
35
- it "returns the number" do
36
- expect(subject_value_for(15)).to eq(15)
37
- end
38
- end
39
-
40
- it "can be overriden and super'd to from a nested group" do
41
- outer_subject_value = inner_subject_value = nil
42
-
43
- ExampleGroup.describe(Array) do
44
- subject { super() << :parent_group }
45
- example { outer_subject_value = subject }
46
-
47
- context "nested" do
48
- subject { super() << :child_group }
49
- example { inner_subject_value = subject }
50
- end
51
- end.run
52
-
53
- expect(outer_subject_value).to eq([:parent_group])
54
- expect(inner_subject_value).to eq([:parent_group, :child_group])
55
- end
56
- end
57
-
58
- describe "explicit subject" do
59
- it "yields the example in which it is eval'd" do
60
- example_yielded_to_subject = nil
61
- example_yielded_to_example = nil
62
-
63
- group = ExampleGroup.describe
64
- group.subject { |e| example_yielded_to_subject = e }
65
- group.example { |e| subject; example_yielded_to_example = e }
66
- group.run
67
-
68
- expect(example_yielded_to_subject).to eq example_yielded_to_example
69
- end
70
-
71
- [false, nil].each do |falsy_value|
72
- context "with a value of #{falsy_value.inspect}" do
73
- it "is evaluated once per example" do
74
- group = ExampleGroup.describe(Array)
75
- group.before do
76
- expect(Object).to receive(:this_question?).once.and_return(falsy_value)
77
- end
78
- group.subject do
79
- Object.this_question?
80
- end
81
- group.example do
82
- subject
83
- subject
84
- end
85
- expect(group.run).to be_truthy, "expected subject block to be evaluated only once"
86
- end
87
- end
88
- end
89
-
90
- describe "defined in a top level group" do
91
- it "replaces the implicit subject in that group" do
92
- subject_value = subject_value_for(Array) do
93
- subject { [1, 2, 3] }
94
- end
95
- expect(subject_value).to eq([1, 2, 3])
96
- end
97
- end
98
-
99
- describe "defined in a top level group" do
100
- let(:group) do
101
- ExampleGroup.describe do
102
- subject{ [4, 5, 6] }
103
- end
104
- end
105
-
106
- it "is available in a nested group (subclass)" do
107
- subject_value = nil
108
- group.describe("I'm nested!") do
109
- example { subject_value = subject }
110
- end.run
111
-
112
- expect(subject_value).to eq([4, 5, 6])
113
- end
114
-
115
- it "is available in a doubly nested group (subclass)" do
116
- subject_value = nil
117
- group.describe("Nesting level 1") do
118
- describe("Nesting level 2") do
119
- example { subject_value = subject }
120
- end
121
- end.run
122
-
123
- expect(subject_value).to eq([4, 5, 6])
124
- end
125
-
126
- it "can be overriden and super'd to from a nested group" do
127
- subject_value = nil
128
- group.describe("Nested") do
129
- subject { super() + [:override] }
130
- example { subject_value = subject }
131
- end.run
132
-
133
- expect(subject_value).to eq([4, 5, 6, :override])
134
- end
135
-
136
- [:before, :after].each do |hook|
137
- it "raises an error when referenced from `#{hook}(:all)`" do
138
- result = nil
139
- line = nil
140
-
141
- ExampleGroup.describe do
142
- subject { nil }
143
- send(hook, :all) { result = (subject rescue $!) }; line = __LINE__
144
- example { }
145
- end.run
146
-
147
- expect(result).to be_an(Exception)
148
- expect(result.message).to match(/subject accessed.*#{hook}\(:all\).*#{__FILE__}:#{line}/m)
149
- end
150
- end
151
- end
152
-
153
- describe "with a name" do
154
- it "yields the example in which it is eval'd" do
155
- example_yielded_to_subject = nil
156
- example_yielded_to_example = nil
157
-
158
- group = ExampleGroup.describe
159
- group.subject(:foo) { |e| example_yielded_to_subject = e }
160
- group.example { |e| foo; example_yielded_to_example = e }
161
- group.run
162
-
163
- expect(example_yielded_to_subject).to eq example_yielded_to_example
164
- end
165
-
166
- it "defines a method that returns the memoized subject" do
167
- list_value_1 = list_value_2 = subject_value_1 = subject_value_2 = nil
168
-
169
- ExampleGroup.describe do
170
- subject(:list) { [1, 2, 3] }
171
- example do
172
- list_value_1 = list
173
- list_value_2 = list
174
- subject_value_1 = subject
175
- subject_value_2 = subject
176
- end
177
- end.run
178
-
179
- expect(list_value_1).to eq([1, 2, 3])
180
- expect(list_value_1).to equal(list_value_2)
181
-
182
- expect(subject_value_1).to equal(subject_value_2)
183
- expect(subject_value_1).to equal(list_value_1)
184
- end
185
-
186
- it "is referred from inside subject by the name" do
187
- inner_subject_value = nil
188
-
189
- ExampleGroup.describe do
190
- subject(:list) { [1, 2, 3] }
191
- describe 'first' do
192
- subject(:first_element) { list.first }
193
- example { inner_subject_value = subject }
194
- end
195
- end.run
196
-
197
- expect(inner_subject_value).to eq(1)
198
- end
199
-
200
- it 'can continue to be referenced by the name even when an inner group redefines the subject' do
201
- named_value = nil
202
-
203
- ExampleGroup.describe do
204
- subject(:named) { :outer }
205
-
206
- describe "inner" do
207
- subject { :inner }
208
- example do
209
- subject # so the inner subject method is run and memoized
210
- named_value = self.named
211
- end
212
- end
213
- end.run
214
-
215
- expect(named_value).to eq(:outer)
216
- end
217
-
218
- it 'can continue to reference an inner subject after the outer subject name is referenced' do
219
- subject_value = nil
220
-
221
- ExampleGroup.describe do
222
- subject(:named) { :outer }
223
-
224
- describe "inner" do
225
- subject { :inner }
226
- example do
227
- named # so the outer subject method is run and memoized
228
- subject_value = self.subject
229
- end
230
- end
231
- end.run
232
-
233
- expect(subject_value).to eq(:inner)
234
- end
235
-
236
- it 'is not overriden when an inner group defines a new method with the same name' do
237
- subject_value = nil
238
-
239
- ExampleGroup.describe do
240
- subject(:named) { :outer_subject }
241
-
242
- describe "inner" do
243
- let(:named) { :inner_named }
244
- example { subject_value = self.subject }
245
- end
246
- end.run
247
-
248
- expect(subject_value).to be(:outer_subject)
249
- end
250
-
251
- context 'when `super` is used' do
252
- def should_raise_not_supported_error(&block)
253
- ex = nil
254
-
255
- ExampleGroup.describe do
256
- let(:list) { ["a", "b", "c"] }
257
- subject { [1, 2, 3] }
258
-
259
- describe 'first' do
260
- module_eval(&block) if block
261
-
262
- subject(:list) { super().first(2) }
263
- ex = example { subject }
264
- end
265
- end.run
266
-
267
- expect(ex.execution_result[:status]).to eq("failed")
268
- expect(ex.execution_result[:exception].message).to match(/super.*not supported/)
269
- end
270
-
271
- it 'raises a "not supported" error' do
272
- should_raise_not_supported_error
273
- end
274
-
275
- context 'with a `let` definition before the named subject' do
276
- it 'raises a "not supported" error' do
277
- should_raise_not_supported_error do
278
- # My first pass implementation worked unless there was a `let`
279
- # declared before the named subject -- this let is in place to
280
- # ensure that bug doesn't return.
281
- let(:foo) { 3 }
282
- end
283
- end
284
- end
285
- end
286
- end
287
- end
288
-
289
- context "using 'self' as an explicit subject" do
290
- it "delegates matcher to the ExampleGroup" do
291
- group = ExampleGroup.describe("group") do
292
- subject { self }
293
- def ok?; true; end
294
- def not_ok?; false; end
295
-
296
- it { should eq(self) }
297
- it { should be_ok }
298
- it { should_not be_not_ok }
299
- end
300
-
301
- expect(group.run).to be true
302
- end
303
-
304
- it 'supports a new expect-based syntax' do
305
- group = ExampleGroup.describe([1, 2, 3]) do
306
- it { is_expected.to be_an Array }
307
- it { is_expected.not_to include 4 }
308
- end
309
-
310
- expect(group.run).to be true
311
- end
312
- end
313
-
314
- describe '#subject!' do
315
- let(:prepared_array) { [1,2,3] }
316
- subject! { prepared_array.pop }
317
-
318
- it "evaluates subject before example" do
319
- expect(prepared_array).to eq([1,2])
320
- end
321
-
322
- it "returns memoized value from first invocation" do
323
- expect(subject).to eq(3)
324
- end
325
- end
326
- end
327
-
328
- RSpec.describe "#let" do
329
- let(:counter) do
330
- Class.new do
331
- def initialize
332
- @count = 0
333
- end
334
- def count
335
- @count += 1
336
- end
337
- end.new
338
- end
339
-
340
- let(:nil_value) do
341
- @nil_value_count += 1
342
- nil
343
- end
344
-
345
- it "generates an instance method" do
346
- expect(counter.count).to eq(1)
347
- end
348
-
349
- it "caches the value" do
350
- expect(counter.count).to eq(1)
351
- expect(counter.count).to eq(2)
352
- end
353
-
354
- it "caches a nil value" do
355
- @nil_value_count = 0
356
- nil_value
357
- nil_value
358
-
359
- expect(@nil_value_count).to eq(1)
360
- end
361
-
362
- let(:yield_the_example) do |example_yielded_to_let|
363
- @example_yielded_to_let = example_yielded_to_let
364
- end
365
-
366
- it "yields the example" do |example_yielded_to_example|
367
- yield_the_example
368
- expect(@example_yielded_to_let).to equal example_yielded_to_example
369
- end
370
-
371
- let(:regex_with_capture) { %r[RegexWithCapture(\d)] }
372
-
373
- it 'does not pass the block up the ancestor chain' do
374
- # Test for Ruby bug http://bugs.ruby-lang.org/issues/8059
375
- expect("RegexWithCapture1".match(regex_with_capture)[1]).to eq('1')
376
- end
377
-
378
- it 'raises a useful error when called without a block' do
379
- expect do
380
- ExampleGroup.describe { let(:list) }
381
- end.to raise_error(/#let or #subject called without a block/)
382
- end
383
-
384
- let(:a_value) { "a string" }
385
-
386
- context 'when overriding let in a nested context' do
387
- let(:a_value) { super() + " (modified)" }
388
-
389
- it 'can use `super` to reference the parent context value' do
390
- expect(a_value).to eq("a string (modified)")
391
- end
392
- end
393
-
394
- context 'when the declaration uses `return`' do
395
- let(:value) do
396
- return :early_exit if @early_exit
397
- :late_exit
398
- end
399
-
400
- it 'can exit the let declaration early' do
401
- @early_exit = true
402
- expect(value).to eq(:early_exit)
403
- end
404
-
405
- it 'can get past a conditional `return` statement' do
406
- @early_exit = false
407
- expect(value).to eq(:late_exit)
408
- end
409
- end
410
-
411
- [:before, :after].each do |hook|
412
- it "raises an error when referenced from `#{hook}(:all)`" do
413
- result = nil
414
- line = nil
415
-
416
- ExampleGroup.describe do
417
- let(:foo) { nil }
418
- send(hook, :all) { result = (foo rescue $!) }; line = __LINE__
419
- example { }
420
- end.run
421
-
422
- expect(result).to be_an(Exception)
423
- expect(result.message).to match(/let declaration `foo` accessed.*#{hook}\(:all\).*#{__FILE__}:#{line}/m)
424
- end
425
- end
426
-
427
- context "when included modules have hooks that define memoized helpers" do
428
- it "allows memoized helpers to override methods in previously included modules" do
429
- group = ExampleGroup.describe do
430
- include Module.new {
431
- def self.included(m); m.let(:unrelated) { :unrelated }; end
432
- }
433
-
434
- include Module.new {
435
- def hello_message; "Hello from module"; end
436
- }
437
-
438
- let(:hello_message) { "Hello from let" }
439
- end
440
-
441
- expect(group.new.hello_message).to eq("Hello from let")
442
- end
443
- end
444
- end
445
-
446
- RSpec.describe "#let!" do
447
- subject { [1,2,3] }
448
- let!(:popped) { subject.pop }
449
-
450
- it "evaluates the value non-lazily" do
451
- expect(subject).to eq([1,2])
452
- end
453
-
454
- it "returns memoized value from first invocation" do
455
- expect(popped).to eq(3)
456
- end
457
- end
458
-
459
- RSpec.describe 'using subject in before and let blocks' do
460
- shared_examples_for 'a subject' do
461
- let(:subject_id_in_let) { subject.object_id }
462
- before { @subject_id_in_before = subject.object_id }
463
-
464
- it 'should be memoized' do
465
- expect(subject_id_in_let).to eq(@subject_id_in_before)
466
- end
467
-
468
- it { is_expected.to eq(subject) }
469
- end
470
-
471
- describe Object do
472
- context 'with implicit subject' do
473
- it_should_behave_like 'a subject'
474
- end
475
-
476
- context 'with explicit subject' do
477
- subject { Object.new }
478
- it_should_behave_like 'a subject'
479
- end
480
-
481
- context 'with a constant subject'do
482
- subject { 123 }
483
- it_should_behave_like 'a subject'
484
- end
485
- end
486
- end
487
-
488
- RSpec.describe 'Module#define_method' do
489
- it 'is still a private method' do
490
- a_module = Module.new
491
- expect { a_module.define_method(:name) { "implementation" } }.to raise_error NoMethodError
492
- end
493
- end
494
- end
495
-