Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / spec / spec / example / example_suite_spec.rb
blob0e694f4b99d3032a9a49e5fd5350bd736a6d73cd
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 module Spec
4   module Example
5     describe ExampleSuite, "#run", :shared => true do
6       before :all do
7         @original_rspec_options = $rspec_options
8       end
10       before :each do
11         @options = ::Spec::Runner::Options.new(StringIO.new, StringIO.new)
12         $rspec_options = @options
13         @formatter = mock("formatter", :null_object => true)
14         @options.formatters << @formatter
15         @options.backtrace_tweaker = mock("backtrace_tweaker", :null_object => true)
16         @reporter = FakeReporter.new(@options)
17         @options.reporter = @reporter
18         @behaviour = Class.new(ExampleGroup) do
19           describe("example")
20           it "does nothing" do
21           end
22         end
23         class << @behaviour
24           public :include
25         end
26       end
28       after :each do
29         $rspec_options = @original_rspec_options
30         ExampleGroup.reset
31       end
32     end
34     describe ExampleSuite, "#run without failure in example", :shared => true do
35       it_should_behave_like "Spec::Example::ExampleSuite#run"
37       it "should not add an example failure to the TestResult" do
38         suite = @behaviour.suite
39         suite.run.should be_true
40       end
41     end
43     describe ExampleSuite, "#run with failure in example", :shared => true do
44       it_should_behave_like "Spec::Example::ExampleSuite#run"
45       
46       it "should add an example failure to the TestResult" do
47         suite = @behaviour.suite
48         suite.run.should be_false
49       end
50     end
52     describe ExampleSuite, "#run on dry run" do
53       it_should_behave_like "Spec::Example::ExampleSuite#run"
55       before do
56         @options.dry_run = true
57       end
58     
59       it "should not run before(:all) or after(:all)" do
60         before_all_ran = false
61         after_all_ran = false
62         ExampleGroup.before(:all) { before_all_ran = true }
63         ExampleGroup.after(:all) { after_all_ran = true }
64         @behaviour.it("should") {}
65         suite = @behaviour.suite
66         suite.run
67         before_all_ran.should be_false
68         after_all_ran.should be_false
69       end
71       it "should not run example" do
72         example_ran = false
73         @behaviour.it("should") {example_ran = true}
74         suite = @behaviour.suite
75         suite.run
76         example_ran.should be_false
77       end
78     end
80     describe ExampleSuite, "#run with success" do
81       it_should_behave_like "Spec::Example::ExampleSuite#run without failure in example"
83       before do
84         @special_behaviour = Class.new(ExampleGroup)
85         ExampleGroupFactory.register(:special, @special_behaviour)
86         @not_special_behaviour = Class.new(ExampleGroup)
87         ExampleGroupFactory.register(:not_special, @not_special_behaviour)
88       end
90       after do
91         ExampleGroupFactory.reset
92       end
94       it "should send reporter add_example_group" do
95         suite = @behaviour.suite
96         suite.run
97         @reporter.added_behaviour.should == "example"
98       end
100       it "should run example on run" do
101         example_ran = false
102         @behaviour.it("should") {example_ran = true}
103         suite = @behaviour.suite
104         suite.run
105         example_ran.should be_true
106       end
108       it "should run before(:all) block only once" do
109         before_all_run_count_run_count = 0
110         @behaviour.before(:all) {before_all_run_count_run_count += 1}
111         @behaviour.it("test") {true}
112         @behaviour.it("test2") {true}
113         suite = @behaviour.suite
114         suite.run
115         before_all_run_count_run_count.should == 1
116       end
118       it "should run after(:all) block only once" do
119         after_all_run_count = 0
120         @behaviour.after(:all) {after_all_run_count += 1}
121         @behaviour.it("test") {true}
122         @behaviour.it("test2") {true}
123         suite = @behaviour.suite
124         suite.run
125         after_all_run_count.should == 1
126         @reporter.rspec_verify
127       end
129       it "after(:all) should have access to all instance variables defined in before(:all)" do
130         context_instance_value_in = "Hello there"
131         context_instance_value_out = ""
132         @behaviour.before(:all) { @instance_var = context_instance_value_in }
133         @behaviour.after(:all) { context_instance_value_out = @instance_var }
134         @behaviour.it("test") {true}
135         suite = @behaviour.suite
136         suite.run
137         context_instance_value_in.should == context_instance_value_out
138       end
140       it "should copy instance variables from before(:all)'s execution context into spec's execution context" do
141         context_instance_value_in = "Hello there"
142         context_instance_value_out = ""
143         @behaviour.before(:all) { @instance_var = context_instance_value_in }
144         @behaviour.it("test") {context_instance_value_out = @instance_var}
145         suite = @behaviour.suite
146         suite.run
147         context_instance_value_in.should == context_instance_value_out
148       end
150       it "should not add global before callbacks for untargetted behaviours" do
151         fiddle = []
153         ExampleGroup.before(:all) { fiddle << "Example.before(:all)" }
154         ExampleGroup.prepend_before(:all) { fiddle << "Example.prepend_before(:all)" }
155         @special_behaviour.before(:each) { fiddle << "Example.before(:each, :behaviour_type => :special)" }
156         @special_behaviour.prepend_before(:each) { fiddle << "Example.prepend_before(:each, :behaviour_type => :special)" }
157         @special_behaviour.before(:all) { fiddle << "Example.before(:all, :behaviour_type => :special)" }
158         @special_behaviour.prepend_before(:all) { fiddle << "Example.prepend_before(:all, :behaviour_type => :special)" }
160         behaviour = Class.new(ExampleGroup) do
161           describe("I'm not special", :behaviour_type => :not_special)
162           it "does nothing"
163         end
164         suite = behaviour.suite
165         suite.run
166         fiddle.should == [
167           'Example.prepend_before(:all)',
168           'Example.before(:all)',
169         ]
170       end
172       it "should add global before callbacks for targetted behaviours" do
173         fiddle = []
175         ExampleGroup.before(:all) { fiddle << "Example.before(:all)" }
176         ExampleGroup.prepend_before(:all) { fiddle << "Example.prepend_before(:all)" }
177         @special_behaviour.before(:each) { fiddle << "special.before(:each, :behaviour_type => :special)" }
178         @special_behaviour.prepend_before(:each) { fiddle << "special.prepend_before(:each, :behaviour_type => :special)" }
179         @special_behaviour.before(:all) { fiddle << "special.before(:all, :behaviour_type => :special)" }
180         @special_behaviour.prepend_before(:all) { fiddle << "special.prepend_before(:all, :behaviour_type => :special)" }
181         @special_behaviour.append_before(:each) { fiddle << "special.append_before(:each, :behaviour_type => :special)" }
182         
183         behaviour = Class.new(@special_behaviour).describe("I'm a special behaviour") {}
184         behaviour.it("test") {true}
185         suite = behaviour.suite
186         suite.run
187         fiddle.should == [
188           'Example.prepend_before(:all)',
189           'Example.before(:all)',
190           'special.prepend_before(:all, :behaviour_type => :special)',
191           'special.before(:all, :behaviour_type => :special)',
192           'special.prepend_before(:each, :behaviour_type => :special)',
193           'special.before(:each, :behaviour_type => :special)',
194           'special.append_before(:each, :behaviour_type => :special)',
195         ]
196       end
198       it "should order before callbacks from global to local" do
199         fiddle = []
200         ExampleGroup.prepend_before(:all) { fiddle << "Example.prepend_before(:all)" }
201         ExampleGroup.before(:all) { fiddle << "Example.before(:all)" }
202         @behaviour.prepend_before(:all) { fiddle << "prepend_before(:all)" }
203         @behaviour.before(:all) { fiddle << "before(:all)" }
204         @behaviour.prepend_before(:each) { fiddle << "prepend_before(:each)" }
205         @behaviour.before(:each) { fiddle << "before(:each)" }
206         suite = @behaviour.suite
207         suite.run
208         fiddle.should == [
209           'Example.prepend_before(:all)',
210           'Example.before(:all)',
211           'prepend_before(:all)',
212           'before(:all)',
213           'prepend_before(:each)',
214           'before(:each)'
215         ]
216       end
218       it "should order after callbacks from local to global" do
219         @reporter.should_receive(:add_example_group).with any_args()
220         @reporter.should_receive(:example_finished).with any_args()
222         fiddle = []
223         @behaviour.after(:each) { fiddle << "after(:each)" }
224         @behaviour.append_after(:each) { fiddle << "append_after(:each)" }
225         @behaviour.after(:all) { fiddle << "after(:all)" }
226         @behaviour.append_after(:all) { fiddle << "append_after(:all)" }
227         ExampleGroup.after(:all) { fiddle << "Example.after(:all)" }
228         ExampleGroup.append_after(:all) { fiddle << "Example.append_after(:all)" }
229         suite = @behaviour.suite
230         suite.run
231         fiddle.should == [
232           'after(:each)',
233           'append_after(:each)',
234           'after(:all)',
235           'append_after(:all)',
236           'Example.after(:all)',
237           'Example.append_after(:all)'
238         ]
239       end
241       it "should have accessible instance methods from included module" do
242         @reporter.should_receive(:add_example_group).with any_args()
243         @reporter.should_receive(:example_finished).with any_args()
245         mod1_method_called = false
246         mod1 = Module.new do
247           define_method :mod1_method do
248             mod1_method_called = true
249           end
250         end
252         mod2_method_called = false
253         mod2 = Module.new do
254           define_method :mod2_method do
255             mod2_method_called = true
256           end
257         end
259         @behaviour.include mod1, mod2
261         @behaviour.it("test") do
262           mod1_method
263           mod2_method
264         end
265         suite = @behaviour.suite
266         suite.run
267         mod1_method_called.should be_true
268         mod2_method_called.should be_true
269       end
271       it "should include targetted modules included using configuration" do
272         mod1 = Module.new
273         mod2 = Module.new
274         mod3 = Module.new
275         Spec::Runner.configuration.include(mod1, mod2)
276         Spec::Runner.configuration.include(mod3, :behaviour_type => :not_special)
278         behaviour = Class.new(@special_behaviour).describe("I'm special", :behaviour_type => :special) do
279           it "does nothing"
280         end
281         suite = behaviour.suite
282         suite.run
284         behaviour.included_modules.should include(mod1)
285         behaviour.included_modules.should include(mod2)
286         behaviour.included_modules.should_not include(mod3)
287       end
289       it "should include any predicate_matchers included using configuration" do
290         $included_predicate_matcher_found = false
291         Spec::Runner.configuration.predicate_matchers[:do_something] = :does_something?
292         behaviour = Class.new(ExampleGroup) do
293           describe('example')
294           it "should respond to do_something" do
295             $included_predicate_matcher_found = respond_to?(:do_something)
296           end
297         end
298         suite = behaviour.suite
299         suite.run
300         $included_predicate_matcher_found.should be(true)
301       end
303       it "should use a mock framework set up in config" do
304         mod = Module.new do
305           class << self
306             def included(mod)
307               $included_module = mod
308             end
309           end
310         end
312         begin
313           $included_module = nil
314           Spec::Runner.configuration.mock_with mod
316           behaviour = Class.new(ExampleGroup) do
317             describe('example')
318             it "does nothing"
319           end
320           suite = behaviour.suite
321           suite.run
323           $included_module.should_not be_nil
324         ensure
325           Spec::Runner.configuration.mock_with :rspec
326         end
327       end
328     end
330     describe ExampleSuite, "#run with pending example that has a failing assertion" do
331       it_should_behave_like "Spec::Example::ExampleSuite#run without failure in example"
333       before do
334         @behaviour.it("should be pending") do
335           pending("Example fails") {false.should be_true}
336         end
337       end
339       it "should send example_pending to formatter" do
340         @formatter.should_receive(:example_pending).with("example", "should be pending", "Example fails")
341         suite = @behaviour.suite
342         suite.run
343       end
344     end
346     describe ExampleSuite, "#run with pending example that does not have a failing assertion" do
347       it_should_behave_like "Spec::Example::ExampleSuite#run with failure in example"
349       before do
350         @behaviour.it("should be pending") do
351           pending("Example passes") {true.should be_true}
352         end
353       end
355       it "should send example_pending to formatter" do
356         @formatter.should_receive(:example_pending).with("example", "should be pending", "Example passes")
357         suite = @behaviour.suite
358         suite.run
359       end
360     end
362     describe ExampleSuite, "#run when before(:all) fails" do
363       it_should_behave_like "Spec::Example::ExampleSuite#run"
365       before do
366         ExampleGroup.before(:all) { raise NonStandardError, "before(:all) failure" }
367       end
369       it "should not run any example" do
370         spec_ran = false
371         @behaviour.it("test") {spec_ran = true}
372         suite = @behaviour.suite
373         suite.run
374         spec_ran.should be_false
375       end
377       it "should run ExampleGroup after(:all)" do
378         after_all_ran = false
379         ExampleGroup.after(:all) { after_all_ran = true }
380         suite = @behaviour.suite
381         suite.run
382         after_all_ran.should be_true
383       end
385       it "should run behaviour after(:all)" do
386         after_all_ran = false
387         @behaviour.after(:all) { after_all_ran = true }
388         suite = @behaviour.suite
389         suite.run
390         after_all_ran.should be_true
391       end
393       it "should supply before(:all) as description" do
394         @reporter.should_receive(:example_finished) do |example, error, location|
395           example.description.should eql("before(:all)")
396           error.message.should eql("before(:all) failure")
397           location.should eql("before(:all)")
398         end
400         @behaviour.it("test") {true}
401         suite = @behaviour.suite
402         suite.run
403       end
404     end
406     describe ExampleSuite, "#run when before(:each) fails" do
407       it_should_behave_like "Spec::Example::ExampleSuite#run with failure in example"
409       before do
410         ExampleGroup.before(:each) { raise NonStandardError }
411       end
412       
413       it "should run after(:all)" do
414         after_all_ran = false
415         ExampleGroup.after(:all) { after_all_ran = true }
416         suite = @behaviour.suite
417         suite.run
418         after_all_ran.should be_true
419       end
420     end
422     describe ExampleSuite, "#run when any example fails" do
423       it_should_behave_like "Spec::Example::ExampleSuite#run with failure in example"
425       before do
426         @behaviour.it("should") { raise NonStandardError }
427       end
428       
429       it "should run after(:all)" do
430         after_all_ran = false
431         ExampleGroup.after(:all) { after_all_ran = true }
432         suite = @behaviour.suite
433         suite.run
434         after_all_ran.should be_true
435       end
436     end
438     describe ExampleSuite, "#run when first after(:each) block fails" do
439       it_should_behave_like "Spec::Example::ExampleSuite#run"
441       it "should add an example failure to the TestResult" do
442         second_after_ran = false
443         @behaviour.after(:each) do
444           second_after_ran = true
445         end
446         first_after_ran = false
447         @behaviour.after(:each) do
448           first_after_ran = true
449           raise "first"
450         end
451         
452         suite = @behaviour.suite
453         suite.run.should be_false
454       end      
455       
456       it "should run second after(:each) block" do
457         second_after_ran = false
458         @behaviour.after(:each) do
459           second_after_ran = true
460         end
461         first_after_ran = false
462         @behaviour.after(:each) do
463           first_after_ran = true
464           raise "first"
465         end
467         @reporter.should_receive(:example_finished) do |example, error, location|
468           example.should equal(example)
469           error.message.should eql("first")
470           location.should eql("after(:each)")
471         end
472         suite = @behaviour.suite
473         suite.run
474         first_after_ran.should be_true
475         second_after_ran.should be_true
476       end
477     end
479     describe ExampleSuite, "#run when first before(:each) block fails" do
480       it_should_behave_like "Spec::Example::ExampleSuite#run"
482       it "should add an example failure to the TestResult" do
483         first_before_ran = false
484         @behaviour.before(:each) do
485           first_before_ran = true
486           raise "first"
487         end
488         second_before_ran = false
489         @behaviour.before(:each) do
490           second_before_ran = true
491         end
492         
493         suite = @behaviour.suite
494         suite.run.should be_false
495       end
497       it "should not run second before(:each)" do
498         first_before_ran = false
499         @behaviour.before(:each) do
500           first_before_ran = true
501           raise "first"
502         end
503         second_before_ran = false
504         @behaviour.before(:each) do
505           second_before_ran = true
506         end
508         @reporter.should_receive(:example_finished) do |name, error, location, example_not_implemented|
509           name.should eql("example")
510           error.message.should eql("first")
511           location.should eql("before(:each)")
512           example_not_implemented.should be_false
513         end
514         suite = @behaviour.suite
515         suite.run
516         first_before_ran.should be_true
517         second_before_ran.should be_false
518       end
519     end
521     describe ExampleSuite, "#run when failure in after(:all)" do
522       it_should_behave_like "Spec::Example::ExampleSuite#run"
524       before do
525         ExampleGroup.after(:all) { raise NonStandardError, "in after(:all)" }
526       end
528       it "should return false" do
529         suite = @behaviour.suite
530         suite.run.should be_false
531       end      
533       it "should provide after(:all) as description" do
534         @reporter.should_receive(:example_finished) do |example, error, location|
535           example.description.should eql("after(:all)")
536           error.message.should eql("in after(:all)")
537           location.should eql("after(:all)")
538         end
540         suite = @behaviour.suite
541         suite.run
542       end
543     end
545     describe ExampleSuite, "#size" do
546       it "returns the number of examples in the behaviour" do
547         behaviour = Class.new(ExampleGroup) do
548           describe("Some Examples")
549           it("does something") {}
550           it("does something else") {}
551         end
552         suite = behaviour.suite
553         suite.size.should == 2
554       end
555     end
557     describe ExampleSuite, "#empty?" do
558       it "when there are examples; returns true" do
559         behaviour = Class.new(ExampleGroup) do
560           describe("Some Examples")
561           it("does something") {}
562         end
563         suite = behaviour.suite
564         suite.size.should be > 0
566         suite.should_not be_empty
567       end
569       it "when there are no examples; returns true" do
570         behaviour = Class.new(ExampleGroup) do
571           describe("Some Examples")
572         end
573         suite = behaviour.suite
574         suite.size.should == 0
576         suite.should be_empty
577       end
578     end
580     describe ExampleSuite, "#delete" do
581       it "removes the passed in example" do
582         behaviour = Class.new(ExampleGroup) do
583           describe("Some Examples")
584           it("does something") {}
585         end
586         suite = behaviour.suite
587         suite.delete(suite.examples.first)
589         suite.should be_empty
590       end
591     end
592   end