Upgraded Rails and RSpec
[monkeycharger.git] / vendor / plugins / rspec / rspec / spec / spec / example / example_group_spec.rb
blobd743bf938000c3036d0270a062088f4fb3adfeea
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 module Spec
4   module Example
5     describe ExampleGroup do
6       attr_reader :options, :example_group, :result, :reporter
7       before :all do
8         @original_rspec_options = $rspec_options
9       end
11       before :each do
12         @options = ::Spec::Runner::Options.new(StringIO.new, StringIO.new)
13         $rspec_options = @options
14         options.formatters << mock("formatter", :null_object => true)
15         options.backtrace_tweaker = mock("backtrace_tweaker", :null_object => true)
16         @reporter = FakeReporter.new(@options)
17         options.reporter = reporter
18         @example_group = Class.new(ExampleGroup) do
19           describe("example")
20           it "does nothing"
21         end
22         class << example_group
23           public :include
24         end
25         @result = nil
26       end
28       after :each do
29         $rspec_options = @original_rspec_options
30         ExampleGroup.reset
31       end
33       describe ExampleGroup, ".describe" do
34         attr_reader :child_example_group
35         before do
36           @child_example_group = @example_group.describe("Another ExampleGroup") do
37             it "should pass" do
38               true.should be_true
39             end
40           end
41         end
43         it "should create a subclass of the ExampleGroup when passed a block" do
44           child_example_group.superclass.should == @example_group
45           @options.example_groups.should include(child_example_group)
46         end
48         it "should not inherit examples" do
49           child_example_group.examples.length.should == 1
50         end
51       end
53       describe ExampleGroup, ".it" do
54         it "should should create an example instance" do
55           lambda {
56             @example_group.it("")
57           }.should change { @example_group.examples.length }.by(1)
58         end
59       end
61       describe ExampleGroup, ".xit" do
62         before(:each) do
63           Kernel.stub!(:warn)
64         end
66         it "should NOT  should create an example instance" do
67           lambda {
68             @example_group.xit("")
69           }.should_not change(@example_group.examples, :length)
70         end
72         it "should warn that it is disabled" do
73           Kernel.should_receive(:warn).with("Example disabled: foo")
74           @example_group.xit("foo")
75         end
76       end
78       describe ExampleGroup, ".suite" do
79         it "should return an empty ExampleSuite when there is no description" do
80           ExampleGroup.description.should be_nil
81           ExampleGroup.suite.should be_instance_of(ExampleSuite)
82           ExampleGroup.suite.tests.should be_empty
83         end
85         it "should return an ExampleSuite with Examples" do
86           behaviour = Class.new(ExampleGroup) do
87             describe('example')
88             it "should pass" do
89               1.should == 1
90             end
91           end
92           suite = behaviour.suite
93           suite.tests.length.should == 1
94           suite.tests.first._example.description.should == "should pass"
95         end
97         it "should include methods that begin with test and has an arity of 0 in suite" do
98           behaviour = Class.new(ExampleGroup) do
99             describe('example')
100             def test_any_args(*args)
101               true.should be_true
102             end
103             def test_something
104               1.should == 1
105             end
106             def test
107               raise "This is not a real test"
108             end
109             def testify
110               raise "This is not a real test"
111             end
112           end
113           suite = behaviour.suite
114           suite.tests.length.should == 2
115           descriptions = suite.tests.collect {|test| test._example.description}.sort
116           descriptions.should == ["test_any_args", "test_something"]
117           suite.run.should be_true
118         end
120         it "should include methods that begin with should and has an arity of 0 in suite" do
121           behaviour = Class.new(ExampleGroup) do
122             describe('example')
123             def shouldCamelCase
124               true.should be_true
125             end
126             def should_any_args(*args)
127               true.should be_true
128             end
129             def should_something
130               1.should == 1
131             end
132             def should_not_something
133               1.should_not == 2
134             end
135             def should
136               raise "This is not a real example"
137             end
138             def should_not
139               raise "This is not a real example"
140             end
141           end
142           behaviour = behaviour.dup
143           suite = behaviour.suite
144           suite.tests.length.should == 4
145           descriptions = suite.tests.collect {|test| test._example.description}.sort
146           descriptions.should include("shouldCamelCase")
147           descriptions.should include("should_any_args")
148           descriptions.should include("should_something")
149           descriptions.should include("should_not_something")
150         end
152         it "should not include methods that begin with test_ and has an arity > 0 in suite" do
153           behaviour = Class.new(ExampleGroup) do
154             describe('example')
155             def test_invalid(foo)
156               1.should == 1
157             end
158             def testInvalidCamelCase(foo)
159               1.should == 1
160             end
161           end
162           suite = behaviour.suite
163           suite.tests.length.should == 0
164         end
166         it "should not include methods that begin with should_ and has an arity > 0 in suite" do
167           behaviour = Class.new(ExampleGroup) do
168             describe('example')
169             def should_invalid(foo)
170               1.should == 1
171             end
172             def shouldInvalidCamelCase(foo)
173               1.should == 1
174             end
175             def should_not_invalid(foo)
176               1.should == 1
177             end
178           end
179           suite = behaviour.suite
180           suite.tests.length.should == 0
181         end
182       end
184       describe ExampleGroup, ".description" do
185         it "should return the same description instance for each call" do
186           @example_group.description.should eql(@example_group.description)
187         end
188       end
190       describe ExampleGroup, ".remove_after" do
191         it "should unregister a given after(:each) block" do
192           after_all_ran = false
193           @example_group.it("example") {}
194           proc = Proc.new { after_all_ran = true }
195           ExampleGroup.after(:each, &proc)
196           suite = @example_group.suite
197           suite.run
198           after_all_ran.should be_true
200           after_all_ran = false
201           ExampleGroup.remove_after(:each, &proc)
202           suite = @example_group.suite
203           suite.run
204           after_all_ran.should be_false
205         end
206       end
208       describe ExampleGroup, ".include" do
209         it "should have accessible class methods from included module" do
210           mod1_method_called = false
211           mod1 = Module.new do
212             class_methods = Module.new do
213               define_method :mod1_method do
214                 mod1_method_called = true
215               end
216             end
218             metaclass.class_eval do
219               define_method(:included) do |receiver|
220                 receiver.extend class_methods
221               end
222             end
223           end
225           mod2_method_called = false
226           mod2 = Module.new do
227             class_methods = Module.new do
228               define_method :mod2_method do
229                 mod2_method_called = true
230               end
231             end
233             metaclass.class_eval do
234               define_method(:included) do |receiver|
235                 receiver.extend class_methods
236               end
237             end
238           end
240           @example_group.include mod1, mod2
242           @example_group.mod1_method
243           @example_group.mod2_method
244           mod1_method_called.should be_true
245           mod2_method_called.should be_true
246         end
247       end
249       describe ExampleGroup, ".number_of_examples" do
250         it "should count number of specs" do
251           proc do
252             @example_group.it("one") {}
253             @example_group.it("two") {}
254             @example_group.it("three") {}
255             @example_group.it("four") {}
256           end.should change {@example_group.number_of_examples}.by(4)
257         end
258       end
260       describe ExampleGroup, ".class_eval" do
261         it "should allow constants to be defined" do
262           behaviour = Class.new(ExampleGroup) do
263             describe('example')
264             FOO = 1
265             it "should reference FOO" do
266               FOO.should == 1
267             end
268           end
269           suite = behaviour.suite
270           suite.run
271           Object.const_defined?(:FOO).should == false
272         end
273       end
275       describe ExampleGroup, '.register' do
276         it "should add ExampleGroup to set of ExampleGroups to be run" do
277           example_group.register
278           options.example_groups.should include(example_group)
279         end
280       end
282       describe ExampleGroup, '.unregister' do
283         before do
284           example_group.register
285           options.example_groups.should include(example_group)
286         end
288         it "should remove ExampleGroup from set of ExampleGroups to be run" do
289           example_group.unregister
290           options.example_groups.should_not include(example_group)
291         end
292       end
293     end
295     class ExampleModuleScopingSpec < ExampleGroup
296       describe ExampleGroup, " via a class definition"
298       module Foo
299         module Bar
300           def self.loaded?
301             true
302           end
303         end
304       end
305       include Foo
307       it "should understand module scoping" do
308         Bar.should be_loaded
309       end
311       @@foo = 1
313       it "should allow class variables to be defined" do
314         @@foo.should == 1
315       end
316     end
318     class ExampleClassVariablePollutionSpec < ExampleGroup
319       describe ExampleGroup, " via a class definition without a class variable"
321       it "should not retain class variables from other Example classes" do
322         proc do
323           @@foo
324         end.should raise_error
325       end
326     end
328     describe ExampleGroup, '.run functional example' do
329       def count
330         @count ||= 0
331         @count = @count + 1
332         @count
333       end
335       before(:all) do
336         count.should == 1
337       end
339       before(:all) do
340         count.should == 2
341       end
343       before(:each) do
344         count.should == 3
345       end
347       before(:each) do
348         count.should == 4
349       end
351       it "should run before(:all), before(:each), example, after(:each), after(:all) in order" do
352         count.should == 5
353       end
355       after(:each) do
356         count.should == 7
357       end
359       after(:each) do
360         count.should == 6
361       end
363       after(:all) do
364         count.should == 9
365       end
367       after(:all) do
368         count.should == 8
369       end
370     end
372     describe ExampleGroup, "#initialize" do
373       the_behaviour = self
374       it "should have copy of behaviour" do
375         the_behaviour.superclass.should == ExampleGroup
376       end
377     end
379     describe ExampleGroup, "#pending" do
380       it "should raise a Pending error when its block fails" do
381         block_ran = false
382         lambda {
383           pending("something") do
384             block_ran = true
385             raise "something wrong with my example"
386           end
387         }.should raise_error(Spec::Example::ExamplePendingError, "something")
388         block_ran.should == true
389       end
391       it "should raise Spec::Example::PendingExampleFixedError when its block does not fail" do
392         block_ran = false
393         lambda {
394           pending("something") do
395             block_ran = true
396           end
397         }.should raise_error(Spec::Example::PendingExampleFixedError, "Expected pending 'something' to fail. No Error was raised.")
398         block_ran.should == true
399       end
400     end
402     describe ExampleGroup, "#run" do
403       before do
404         @options = ::Spec::Runner::Options.new(StringIO.new, StringIO.new)
405         @original_rspec_options = $rspec_options
406         $rspec_options = @options
407       end
409       after do
410         $rspec_options = @original_rspec_options
411       end
413       it "should not run when there are no examples" do
414         behaviour = Class.new(ExampleGroup) do
415           describe("Foobar")
416         end
417         behaviour.examples.should be_empty
419         reporter = mock("Reporter")
420         reporter.should_not_receive(:add_example_group)
421         suite = behaviour.suite
422         suite.run
423       end
424     end
426     class ExampleSubclass < ExampleGroup
427     end
429     describe ExampleGroup, "subclasses" do
430       after do
431         ExampleGroupFactory.reset
432       end
434       it "should have access to the described_type" do
435         behaviour = Class.new(ExampleSubclass) do
436           describe(Array)
437         end
438         behaviour.send(:described_type).should == Array
439       end
440     end
442     describe Enumerable do
443       def each(&block)
444         ["4", "2", "1"].each(&block)
445       end
447       it "should be included in examples because it is a module" do
448         map{|e| e.to_i}.should == [4,2,1]
449       end
450     end
452     describe "An", Enumerable, "as a second argument" do
453       def each(&block)
454         ["4", "2", "1"].each(&block)
455       end
457       it "should be included in examples because it is a module" do
458         map{|e| e.to_i}.should == [4,2,1]
459       end
460     end
462     describe String do
463       it "should not be included in examples because it is not a module" do
464         lambda{self.map}.should raise_error(NoMethodError, /undefined method `map' for/)
465       end
466     end
467   end