/
example_group_spec.rb
1138 lines (947 loc) · 35 KB
/
example_group_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require 'spec_helper'
class SelfObserver
def self.cache
@cache ||= []
end
def initialize
self.class.cache << self
end
end
module RSpec::Core
describe ExampleGroup do
it_behaves_like "metadata hash builder" do
def metadata_hash(*args)
group = ExampleGroup.describe('example description', *args)
group.metadata
end
end
context 'when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to false' do
before(:each) do
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = false }
end
it 'processes string args as part of the description' do
group = ExampleGroup.describe("some", "separate", "strings")
group.description.should eq("some separate strings")
end
it 'processes symbol args as part of the description' do
Kernel.stub(:warn) # to silence Symbols as args warning
group = ExampleGroup.describe(:some, :separate, :symbols)
group.description.should eq("some separate symbols")
end
end
context 'when RSpec.configuration.treat_symbols_as_metadata_keys_with_true_values is set to true' do
let(:group) { ExampleGroup.describe(:symbol) }
before(:each) do
RSpec.configure { |c| c.treat_symbols_as_metadata_keys_with_true_values = true }
end
it 'does not treat the first argument as a metadata key even if it is a symbol' do
group.metadata.should_not include(:symbol)
end
it 'treats the first argument as part of the description when it is a symbol' do
group.description.should eq("symbol")
end
end
describe "top level group" do
it "runs its children" do
examples_run = []
group = ExampleGroup.describe("parent") do
describe("child") do
it "does something" do
examples_run << example
end
end
end
group.run
examples_run.should have(1).example
end
context "with a failure in the top level group" do
it "runs its children " do
examples_run = []
group = ExampleGroup.describe("parent") do
it "fails" do
examples_run << example
raise "fail"
end
describe("child") do
it "does something" do
examples_run << example
end
end
end
group.run
examples_run.should have(2).examples
end
end
describe "descendants" do
it "returns self + all descendants" do
group = ExampleGroup.describe("parent") do
describe("child") do
describe("grandchild 1") {}
describe("grandchild 2") {}
end
end
group.descendants.size.should eq(4)
end
end
end
describe "child" do
it "is known by parent" do
parent = ExampleGroup.describe
child = parent.describe
parent.children.should eq([child])
end
it "is not registered in world" do
world = RSpec::Core::World.new
parent = ExampleGroup.describe
world.register(parent)
parent.describe
world.example_groups.should eq([parent])
end
end
describe "filtering" do
let(:world) { World.new }
shared_examples "matching filters" do
context "inclusion" do
before do
filter_manager = FilterManager.new
filter_manager.include filter_metadata
world.stub(:filter_manager => filter_manager)
end
it "includes examples in groups matching filter" do
group = ExampleGroup.describe("does something", spec_metadata)
group.stub(:world) { world }
all_examples = [ group.example("first"), group.example("second") ]
group.filtered_examples.should eq(all_examples)
end
it "includes examples directly matching filter" do
group = ExampleGroup.describe("does something")
group.stub(:world) { world }
filtered_examples = [
group.example("first", spec_metadata),
group.example("second", spec_metadata)
]
group.example("third (not-filtered)")
group.filtered_examples.should eq(filtered_examples)
end
end
context "exclusion" do
before do
filter_manager = FilterManager.new
filter_manager.exclude filter_metadata
world.stub(:filter_manager => filter_manager)
end
it "excludes examples in groups matching filter" do
group = ExampleGroup.describe("does something", spec_metadata)
group.stub(:world) { world }
[ group.example("first"), group.example("second") ]
group.filtered_examples.should be_empty
end
it "excludes examples directly matching filter" do
group = ExampleGroup.describe("does something")
group.stub(:world) { world }
[
group.example("first", spec_metadata),
group.example("second", spec_metadata)
]
unfiltered_example = group.example("third (not-filtered)")
group.filtered_examples.should eq([unfiltered_example])
end
end
end
context "matching false" do
let(:spec_metadata) { { :awesome => false }}
context "against false" do
let(:filter_metadata) { { :awesome => false }}
include_examples "matching filters"
end
context "against 'false'" do
let(:filter_metadata) { { :awesome => 'false' }}
include_examples "matching filters"
end
context "against :false" do
let(:filter_metadata) { { :awesome => :false }}
include_examples "matching filters"
end
end
context "matching true" do
let(:spec_metadata) { { :awesome => true }}
context "against true" do
let(:filter_metadata) { { :awesome => true }}
include_examples "matching filters"
end
context "against 'true'" do
let(:filter_metadata) { { :awesome => 'true' }}
include_examples "matching filters"
end
context "against :true" do
let(:filter_metadata) { { :awesome => :true }}
include_examples "matching filters"
end
end
context "matching a string" do
let(:spec_metadata) { { :type => 'special' }}
context "against a string" do
let(:filter_metadata) { { :type => 'special' }}
include_examples "matching filters"
end
context "against a symbol" do
let(:filter_metadata) { { :type => :special }}
include_examples "matching filters"
end
end
context "matching a symbol" do
let(:spec_metadata) { { :type => :special }}
context "against a string" do
let(:filter_metadata) { { :type => 'special' }}
include_examples "matching filters"
end
context "against a symbol" do
let(:filter_metadata) { { :type => :special }}
include_examples "matching filters"
end
end
context "with no filters" do
it "returns all" do
group = ExampleGroup.describe
group.stub(:world) { world }
example = group.example("does something")
group.filtered_examples.should eq([example])
end
end
context "with no examples or groups that match filters" do
it "returns none" do
filter_manager = FilterManager.new
filter_manager.include :awesome => false
world.stub(:filter_manager => filter_manager)
group = ExampleGroup.describe
group.stub(:world) { world }
group.example("does something")
group.filtered_examples.should eq([])
end
end
end
describe '#described_class' do
context "with a constant as the first parameter" do
it "is that constant" do
ExampleGroup.describe(Object) { }.described_class.should eq(Object)
end
end
context "with a string as the first parameter" do
it "is nil" do
ExampleGroup.describe("i'm a computer") { }.described_class.should be_nil
end
end
context "with a constant in an outer group" do
context "and a string in an inner group" do
it "is the top level constant" do
group = ExampleGroup.describe(String) do
describe :symbol do
example "described_class is String" do
described_class.should eq(String)
end
end
end
group.run.should be_true
end
end
end
context "in a nested group" do
it "inherits the described class/module from the outer group" do
group = ExampleGroup.describe(String) do
describe Array do
example "desribes is String" do
described_class.should eq(String)
end
end
end
group.run.should be_true, "expected examples in group to pass"
end
end
end
describe '#described_class' do
it "is the same as described_class" do
self.class.described_class.should eq(self.class.described_class)
end
end
describe '#description' do
it "grabs the description from the metadata" do
group = ExampleGroup.describe(Object, "my desc") { }
group.description.should eq(group.metadata[:example_group][:description])
end
end
describe '#metadata' do
it "adds the third parameter to the metadata" do
ExampleGroup.describe(Object, nil, 'foo' => 'bar') { }.metadata.should include({ "foo" => 'bar' })
end
it "adds the the file_path to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:file_path].should eq(__FILE__)
end
it "has a reader for file_path" do
ExampleGroup.describe(Object) { }.file_path.should eq(__FILE__)
end
it "adds the line_number to metadata" do
ExampleGroup.describe(Object) { }.metadata[:example_group][:line_number].should eq(__LINE__)
end
end
[:focus, :focused].each do |example_alias|
describe "##{example_alias}" do
let(:group) { ExampleGroup.describe }
subject { group.send example_alias, "a focused example" }
it 'defines an example that can be filtered with :focused => true' do
subject.metadata.should include(:focused => true)
end
it 'defines an example that can be filtered with :focus => true' do
subject.metadata.should include(:focus => true)
end
end
end
describe "#before, after, and around hooks" do
it "runs the before alls in order" do
group = ExampleGroup.describe
order = []
group.before(:all) { order << 1 }
group.before(:all) { order << 2 }
group.before(:all) { order << 3 }
group.example("example") {}
group.run
order.should eq([1,2,3])
end
it "runs the before eachs in order" do
group = ExampleGroup.describe
order = []
group.before(:each) { order << 1 }
group.before(:each) { order << 2 }
group.before(:each) { order << 3 }
group.example("example") {}
group.run
order.should eq([1,2,3])
end
it "runs the after eachs in reverse order" do
group = ExampleGroup.describe
order = []
group.after(:each) { order << 1 }
group.after(:each) { order << 2 }
group.after(:each) { order << 3 }
group.example("example") {}
group.run
order.should eq([3,2,1])
end
it "runs the after alls in reverse order" do
group = ExampleGroup.describe
order = []
group.after(:all) { order << 1 }
group.after(:all) { order << 2 }
group.after(:all) { order << 3 }
group.example("example") {}
group.run
order.should eq([3,2,1])
end
it "only runs before/after(:all) hooks from example groups that have specs that run" do
hooks_run = []
RSpec.configure do |c|
c.filter_run :focus => true
end
unfiltered_group = ExampleGroup.describe "unfiltered" do
before(:all) { hooks_run << :unfiltered_before_all }
after(:all) { hooks_run << :unfiltered_after_all }
context "a subcontext" do
it("has an example") { }
end
end
filtered_group = ExampleGroup.describe "filtered", :focus => true do
before(:all) { hooks_run << :filtered_before_all }
after(:all) { hooks_run << :filtered_after_all }
context "a subcontext" do
it("has an example") { }
end
end
unfiltered_group.run
filtered_group.run
hooks_run.should eq([:filtered_before_all, :filtered_after_all])
end
it "runs before_all_defined_in_config, before all, before each, example, after each, after all, after_all_defined_in_config in that order" do
order = []
RSpec.configure do |c|
c.before(:all) { order << :before_all_defined_in_config }
c.after(:all) { order << :after_all_defined_in_config }
end
group = ExampleGroup.describe
group.before(:all) { order << :top_level_before_all }
group.before(:each) { order << :before_each }
group.after(:each) { order << :after_each }
group.after(:all) { order << :top_level_after_all }
group.example("top level example") { order << :top_level_example }
context1 = group.describe("context 1")
context1.before(:all) { order << :nested_before_all }
context1.example("nested example 1") { order << :nested_example_1 }
context2 = group.describe("context 2")
context2.after(:all) { order << :nested_after_all }
context2.example("nested example 2") { order << :nested_example_2 }
group.run
order.should eq([
:before_all_defined_in_config,
:top_level_before_all,
:before_each,
:top_level_example,
:after_each,
:nested_before_all,
:before_each,
:nested_example_1,
:after_each,
:before_each,
:nested_example_2,
:after_each,
:nested_after_all,
:top_level_after_all,
:after_all_defined_in_config
])
end
context "after(:all)" do
let(:outer) { ExampleGroup.describe }
let(:inner) { outer.describe }
it "has access to state defined before(:all)" do
outer.before(:all) { @outer = "outer" }
inner.before(:all) { @inner = "inner" }
outer.after(:all) do
@outer.should eq("outer")
@inner.should eq("inner")
end
inner.after(:all) do
@inner.should eq("inner")
@outer.should eq("outer")
end
outer.run
end
it "cleans up ivars in after(:all)" do
outer.before(:all) { @outer = "outer" }
inner.before(:all) { @inner = "inner" }
outer.run
inner.before_all_ivars[:@inner].should be_nil
inner.before_all_ivars[:@outer].should be_nil
outer.before_all_ivars[:@inner].should be_nil
outer.before_all_ivars[:@outer].should be_nil
end
end
it "treats an error in before(:each) as a failure" do
group = ExampleGroup.describe
group.before(:each) { raise "error in before each" }
example = group.example("equality") { 1.should eq(2) }
group.run.should be(false)
example.metadata[:execution_result][:exception].message.should eq("error in before each")
end
it "treats an error in before(:all) as a failure" do
group = ExampleGroup.describe
group.before(:all) { raise "error in before all" }
example = group.example("equality") { 1.should eq(2) }
group.run.should be_false
example.metadata.should_not be_nil
example.metadata[:execution_result].should_not be_nil
example.metadata[:execution_result][:exception].should_not be_nil
example.metadata[:execution_result][:exception].message.should eq("error in before all")
end
it "treats an error in before(:all) as a failure for a spec in a nested group" do
example = nil
group = ExampleGroup.describe do
before(:all) { raise "error in before all" }
describe "nested" do
example = it("equality") { 1.should eq(2) }
end
end
group.run
example.metadata.should_not be_nil
example.metadata[:execution_result].should_not be_nil
example.metadata[:execution_result][:exception].should_not be_nil
example.metadata[:execution_result][:exception].message.should eq("error in before all")
end
context "when an error occurs in an after(:all) hook" do
before(:each) do
RSpec.configuration.reporter.stub(:message)
end
let(:group) do
ExampleGroup.describe do
after(:all) { raise "error in after all" }
it("equality") { 1.should eq(1) }
end
end
it "allows the example to pass" do
group.run
example = group.examples.first
example.metadata.should_not be_nil
example.metadata[:execution_result].should_not be_nil
example.metadata[:execution_result][:status].should eq("passed")
end
it "rescues the error and prints it out" do
RSpec.configuration.reporter.should_receive(:message).with(/error in after all/)
group.run
end
end
it "has no 'running example' within before(:all)" do
group = ExampleGroup.describe
running_example = :none
group.before(:all) { running_example = example }
group.example("no-op") { }
group.run
running_example.should be(nil)
end
it "has access to example options within before(:each)" do
group = ExampleGroup.describe
option = nil
group.before(:each) { option = example.options[:data] }
group.example("no-op", :data => :sample) { }
group.run
option.should eq(:sample)
end
it "has access to example options within after(:each)" do
group = ExampleGroup.describe
option = nil
group.after(:each) { option = example.options[:data] }
group.example("no-op", :data => :sample) { }
group.run
option.should eq(:sample)
end
it "has no 'running example' within after(:all)" do
group = ExampleGroup.describe
running_example = :none
group.after(:all) { running_example = example }
group.example("no-op") { }
group.run
running_example.should be(nil)
end
end
matcher :add_a_pending_example_with do |method_name|
match do |group|
group = ExampleGroup.describe
group.send(method_name, "is pending") { }
group.run
group.examples.first.should be_pending
end
end
%w[pending xit xspecify xexample].each do |method_name|
describe "##{method_name}" do
it "generates a pending example" do
ExampleGroup.describe.should add_a_pending_example_with(method_name)
end
end
end
describe "adding examples" do
it "allows adding an example using 'it'" do
group = ExampleGroup.describe
group.it("should do something") { }
group.examples.size.should eq(1)
end
it "exposes all examples at examples" do
group = ExampleGroup.describe
group.it("should do something 1") { }
group.it("should do something 2") { }
group.it("should do something 3") { }
group.should have(3).examples
end
it "maintains the example order" do
group = ExampleGroup.describe
group.it("should 1") { }
group.it("should 2") { }
group.it("should 3") { }
group.examples[0].description.should eq('should 1')
group.examples[1].description.should eq('should 2')
group.examples[2].description.should eq('should 3')
end
end
describe Object, "describing nested example_groups", :little_less_nested => 'yep' do
describe "A sample nested group", :nested_describe => "yep" do
it "sets the described class to the described class of the outer most group" do
example.example_group.described_class.should eq(ExampleGroup)
end
it "sets the description to 'A sample nested describe'" do
example.example_group.description.should eq('A sample nested group')
end
it "has top level metadata from the example_group and its ancestors" do
example.example_group.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
it "exposes the parent metadata to the contained examples" do
example.metadata.should include(:little_less_nested => 'yep', :nested_describe => 'yep')
end
end
end
describe "#run_examples" do
let(:reporter) { double("reporter").as_null_object }
it "returns true if all examples pass" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should eq(1) }
example('ex 2') { 1.should eq(1) }
end
group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
group.run(reporter).should be_true
end
it "returns false if any of the examples fail" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should eq(1) }
example('ex 2') { 1.should eq(2) }
end
group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
group.run(reporter).should be_false
end
it "runs all examples, regardless of any of them failing" do
group = ExampleGroup.describe('group') do
example('ex 1') { 1.should eq(2) }
example('ex 2') { 1.should eq(1) }
end
group.stub(:filtered_examples) { group.examples.extend(Extensions::Ordered) }
group.filtered_examples.each do |example|
example.should_receive(:run)
end
group.run(reporter).should be_false
end
end
describe "how instance variables are inherited" do
before(:all) do
@before_all_top_level = 'before_all_top_level'
end
before(:each) do
@before_each_top_level = 'before_each_top_level'
end
it "can access a before each ivar at the same level" do
@before_each_top_level.should eq('before_each_top_level')
end
it "can access a before all ivar at the same level" do
@before_all_top_level.should eq('before_all_top_level')
end
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.8 do
example.example_group.before_all_ivars.should include('@before_all_top_level' => 'before_all_top_level')
end
it "can access the before all ivars in the before_all_ivars hash", :ruby => 1.9 do
example.example_group.before_all_ivars.should include(:@before_all_top_level => 'before_all_top_level')
end
describe "but now I am nested" do
it "can access a parent example groups before each ivar at a nested level" do
@before_each_top_level.should eq('before_each_top_level')
end
it "can access a parent example groups before all ivar at a nested level" do
@before_all_top_level.should eq("before_all_top_level")
end
it "changes to before all ivars from within an example do not persist outside the current describe" do
@before_all_top_level = "ive been changed"
end
describe "accessing a before_all ivar that was changed in a parent example_group" do
it "does not have access to the modified version" do
@before_all_top_level.should eq('before_all_top_level')
end
end
end
end
describe "ivars are not shared across examples" do
it "(first example)" do
@a = 1
defined?(@b).should be_false
end
it "(second example)" do
@b = 2
defined?(@a).should be_false
end
end
describe "#top_level_description" do
it "returns the description from the outermost example group" do
group = nil
ExampleGroup.describe("top") do
context "middle" do
group = describe "bottom" do
end
end
end
group.top_level_description.should eq("top")
end
end
describe "#run" do
let(:reporter) { double("reporter").as_null_object }
context "with fail_fast? => true" do
it "does not run examples after the failed example" do
group = RSpec::Core::ExampleGroup.describe
group.stub(:fail_fast?) { true }
examples_run = []
group.example('example 1') { examples_run << self }
group.example('example 2') { examples_run << self; fail; }
group.example('example 3') { examples_run << self }
group.run
examples_run.length.should eq(2)
end
end
context "with RSpec.wants_to_quit=true" do
let(:group) { RSpec::Core::ExampleGroup.describe }
before do
RSpec.stub(:wants_to_quit) { true }
RSpec.stub(:clear_remaining_example_groups)
end
it "returns without starting the group" do
reporter.should_not_receive(:example_group_started)
group.run(reporter)
end
context "at top level" do
it "purges remaining groups" do
RSpec.should_receive(:clear_remaining_example_groups)
group.run(reporter)
end
end
context "in a nested group" do
it "does not purge remaining groups" do
nested_group = group.describe
RSpec.should_not_receive(:clear_remaining_example_groups)
nested_group.run(reporter)
end
end
end
context "with all examples passing" do
it "returns true" do
group = describe("something") do
it "does something" do
# pass
end
describe "nested" do
it "does something else" do
# pass
end
end
end
group.run(reporter).should be_true
end
end
context "with top level example failing" do
it "returns false" do
group = describe("something") do
it "does something (wrong - fail)" do
raise "fail"
end
describe "nested" do
it "does something else" do
# pass
end
end
end
group.run(reporter).should be_false
end
end
context "with nested example failing" do
it "returns true" do
group = describe("something") do
it "does something" do
# pass
end
describe "nested" do
it "does something else (wrong -fail)" do
raise "fail"
end
end
end
group.run(reporter).should be_false
end
end
end
describe "#include_context" do
before do
shared_context "named this" do
def foo; 'foo'; end
end
end
it "includes the named context" do
group = ExampleGroup.describe do
include_context "named this"
it "accesses foo" do
foo.should eq('foo')
end
end
group.run.should be_true
end
it "raises a helpful error message when shared context is not found" do
expect do
ExampleGroup.describe do
include_context "shared stuff"
end
end.to raise_error(ArgumentError,%q|Could not find shared context "shared stuff"|)
end
context "given some parameters" do
it "passes the parameters to the shared context" do
passed_params = {}
shared_context "named this with params" do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
end
group = ExampleGroup.describe do
include_context "named this with params", :value1, :value2
end
group.run
passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
end
end
context "given a block" do
it "warns the user that blocks are not supported" do
group = ExampleGroup.describe do
self.should_receive(:warn).with(/blocks not supported for include_context/)
include_context "named this with block" do
true
end
end
group.run
end
end
end
describe "#include_examples" do
before do
shared_examples "named this" do
example("does something") do
end
end
end
it "includes the named examples" do
group = ExampleGroup.describe do
include_examples "named this"
end
group.examples.first.description.should eq("does something")
end
it "raises a helpful error message when shared context is not found" do
expect do
ExampleGroup.describe do
include_examples "shared stuff"
end
end.to raise_error(ArgumentError,%q|Could not find shared examples "shared stuff"|)
end
context "given some parameters" do
it "passes the parameters to the named examples" do
passed_params = {}
shared_examples "named this with params" do |param1, param2|
it("has access to the given parameters") do
passed_params[:param1] = param1
passed_params[:param2] = param2
end
end
group = ExampleGroup.describe do
include_examples "named this with params", :value1, :value2
end
group.run
passed_params.should eq({ :param1 => :value1, :param2 => :value2 })
end
it "adds shared instance methods to the group" do
shared_examples "named this with params" do |param1|
def foo; end
end