1 require File.dirname(__FILE__) + '/../../spec_helper'
5 describe ExampleSuite, "#run", :shared => true do
7 @original_rspec_options = $rspec_options
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
29 $rspec_options = @original_rspec_options
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
43 describe ExampleSuite, "#run with failure in example", :shared => true do
44 it_should_behave_like "Spec::Example::ExampleSuite#run"
46 it "should add an example failure to the TestResult" do
47 suite = @behaviour.suite
48 suite.run.should be_false
52 describe ExampleSuite, "#run on dry run" do
53 it_should_behave_like "Spec::Example::ExampleSuite#run"
56 @options.dry_run = true
59 it "should not run before(:all) or after(:all)" do
60 before_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
67 before_all_ran.should be_false
68 after_all_ran.should be_false
71 it "should not run example" do
73 @behaviour.it("should") {example_ran = true}
74 suite = @behaviour.suite
76 example_ran.should be_false
80 describe ExampleSuite, "#run with success" do
81 it_should_behave_like "Spec::Example::ExampleSuite#run without failure in example"
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)
91 ExampleGroupFactory.reset
94 it "should send reporter add_example_group" do
95 suite = @behaviour.suite
97 @reporter.added_behaviour.should == "example"
100 it "should run example on run" do
102 @behaviour.it("should") {example_ran = true}
103 suite = @behaviour.suite
105 example_ran.should be_true
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
115 before_all_run_count_run_count.should == 1
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
125 after_all_run_count.should == 1
126 @reporter.rspec_verify
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
137 context_instance_value_in.should == context_instance_value_out
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
147 context_instance_value_in.should == context_instance_value_out
150 it "should not add global before callbacks for untargetted behaviours" do
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)
164 suite = behaviour.suite
167 'Example.prepend_before(:all)',
168 'Example.before(:all)',
172 it "should add global before callbacks for targetted behaviours" do
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)" }
183 behaviour = Class.new(@special_behaviour).describe("I'm a special behaviour") {}
184 behaviour.it("test") {true}
185 suite = behaviour.suite
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)',
198 it "should order before callbacks from global to local" do
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
209 'Example.prepend_before(:all)',
210 'Example.before(:all)',
211 'prepend_before(:all)',
213 'prepend_before(:each)',
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()
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
233 'append_after(:each)',
235 'append_after(:all)',
236 'Example.after(:all)',
237 'Example.append_after(:all)'
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
247 define_method :mod1_method do
248 mod1_method_called = true
252 mod2_method_called = false
254 define_method :mod2_method do
255 mod2_method_called = true
259 @behaviour.include mod1, mod2
261 @behaviour.it("test") do
265 suite = @behaviour.suite
267 mod1_method_called.should be_true
268 mod2_method_called.should be_true
271 it "should include targetted modules included using configuration" do
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
281 suite = behaviour.suite
284 behaviour.included_modules.should include(mod1)
285 behaviour.included_modules.should include(mod2)
286 behaviour.included_modules.should_not include(mod3)
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
294 it "should respond to do_something" do
295 $included_predicate_matcher_found = respond_to?(:do_something)
298 suite = behaviour.suite
300 $included_predicate_matcher_found.should be(true)
303 it "should use a mock framework set up in config" do
307 $included_module = mod
313 $included_module = nil
314 Spec::Runner.configuration.mock_with mod
316 behaviour = Class.new(ExampleGroup) do
320 suite = behaviour.suite
323 $included_module.should_not be_nil
325 Spec::Runner.configuration.mock_with :rspec
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"
334 @behaviour.it("should be pending") do
335 pending("Example fails") {false.should be_true}
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
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"
350 @behaviour.it("should be pending") do
351 pending("Example passes") {true.should be_true}
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
362 describe ExampleSuite, "#run when before(:all) fails" do
363 it_should_behave_like "Spec::Example::ExampleSuite#run"
366 ExampleGroup.before(:all) { raise NonStandardError, "before(:all) failure" }
369 it "should not run any example" do
371 @behaviour.it("test") {spec_ran = true}
372 suite = @behaviour.suite
374 spec_ran.should be_false
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
382 after_all_ran.should be_true
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
390 after_all_ran.should be_true
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)")
400 @behaviour.it("test") {true}
401 suite = @behaviour.suite
406 describe ExampleSuite, "#run when before(:each) fails" do
407 it_should_behave_like "Spec::Example::ExampleSuite#run with failure in example"
410 ExampleGroup.before(:each) { raise NonStandardError }
413 it "should run after(:all)" do
414 after_all_ran = false
415 ExampleGroup.after(:all) { after_all_ran = true }
416 suite = @behaviour.suite
418 after_all_ran.should be_true
422 describe ExampleSuite, "#run when any example fails" do
423 it_should_behave_like "Spec::Example::ExampleSuite#run with failure in example"
426 @behaviour.it("should") { raise NonStandardError }
429 it "should run after(:all)" do
430 after_all_ran = false
431 ExampleGroup.after(:all) { after_all_ran = true }
432 suite = @behaviour.suite
434 after_all_ran.should be_true
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
446 first_after_ran = false
447 @behaviour.after(:each) do
448 first_after_ran = true
452 suite = @behaviour.suite
453 suite.run.should be_false
456 it "should run second after(:each) block" do
457 second_after_ran = false
458 @behaviour.after(:each) do
459 second_after_ran = true
461 first_after_ran = false
462 @behaviour.after(:each) do
463 first_after_ran = true
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)")
472 suite = @behaviour.suite
474 first_after_ran.should be_true
475 second_after_ran.should be_true
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
488 second_before_ran = false
489 @behaviour.before(:each) do
490 second_before_ran = true
493 suite = @behaviour.suite
494 suite.run.should be_false
497 it "should not run second before(:each)" do
498 first_before_ran = false
499 @behaviour.before(:each) do
500 first_before_ran = true
503 second_before_ran = false
504 @behaviour.before(:each) do
505 second_before_ran = true
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
514 suite = @behaviour.suite
516 first_before_ran.should be_true
517 second_before_ran.should be_false
521 describe ExampleSuite, "#run when failure in after(:all)" do
522 it_should_behave_like "Spec::Example::ExampleSuite#run"
525 ExampleGroup.after(:all) { raise NonStandardError, "in after(:all)" }
528 it "should return false" do
529 suite = @behaviour.suite
530 suite.run.should be_false
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)")
540 suite = @behaviour.suite
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") {}
552 suite = behaviour.suite
553 suite.size.should == 2
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") {}
563 suite = behaviour.suite
564 suite.size.should be > 0
566 suite.should_not be_empty
569 it "when there are no examples; returns true" do
570 behaviour = Class.new(ExampleGroup) do
571 describe("Some Examples")
573 suite = behaviour.suite
574 suite.size.should == 0
576 suite.should be_empty
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") {}
586 suite = behaviour.suite
587 suite.delete(suite.examples.first)
589 suite.should be_empty