1 require File.dirname(__FILE__) + '/../../spec_helper'
5 describe 'ExampleGroupMethods' do
6 it_should_behave_like "sandboxed rspec_options"
7 attr_reader :example_group, :result, :reporter
9 options.formatters << mock("formatter", :null_object => true)
10 options.backtrace_tweaker = mock("backtrace_tweaker", :null_object => true)
11 @reporter = FakeReporter.new(@options)
12 options.reporter = reporter
13 @example_group = Class.new(ExampleGroup) do
14 describe("ExampleGroup")
17 class << example_group
27 describe "#describe" do
28 attr_reader :child_example_group
30 @child_example_group = @example_group.describe("Another ExampleGroup") do
37 it "should create a subclass of the ExampleGroup when passed a block" do
38 child_example_group.superclass.should == @example_group
39 @options.example_groups.should include(child_example_group)
42 it "should not inherit examples" do
43 child_example_group.examples.length.should == 1
48 it "should should create an example instance" do
51 }.should change { @example_group.examples.length }.by(1)
60 it "should NOT should create an example instance" do
62 @example_group.xit("")
63 }.should_not change(@example_group.examples, :length)
66 it "should warn that it is disabled" do
67 Kernel.should_receive(:warn).with("Example disabled: foo")
68 @example_group.xit("foo")
72 describe "#examples" do
73 it "should have Examples" do
74 example_group = Class.new(ExampleGroup) do
80 example_group.examples.length.should == 1
81 example_group.examples.first.description.should == "should pass"
84 it "should not include methods that begin with test (only when TU interop is loaded)" do
85 example_group = Class.new(ExampleGroup) do
87 def test_any_args(*args)
94 raise "This is not a real test"
97 raise "This is not a real test"
100 example_group.examples.length.should == 0
101 example_group.run.should be_true
104 it "should include methods that begin with should and has an arity of 0 in suite" do
105 example_group = Class.new(ExampleGroup) do
110 def should_any_args(*args)
116 def should_not_something
120 raise "This is not a real example"
123 raise "This is not a real example"
126 example_group = example_group.dup
127 example_group.examples.length.should == 4
128 descriptions = example_group.examples.collect {|example| example.description}.sort
129 descriptions.should include("shouldCamelCase")
130 descriptions.should include("should_any_args")
131 descriptions.should include("should_something")
132 descriptions.should include("should_not_something")
135 it "should not include methods that begin with test_ and has an arity > 0 in suite" do
136 example_group = Class.new(ExampleGroup) do
138 def test_invalid(foo)
141 def testInvalidCamelCase(foo)
145 example_group.examples.length.should == 0
148 it "should not include methods that begin with should_ and has an arity > 0 in suite" do
149 example_group = Class.new(ExampleGroup) do
151 def should_invalid(foo)
154 def shouldInvalidCamelCase(foo)
157 def should_not_invalid(foo)
164 example_group.examples.length.should == 1
165 example_group.run.should be_true
168 it "should run should_methods" do
169 example_group = Class.new(ExampleGroup) do
174 example_group.examples.length.should == 1
175 example_group.run.should be_false
179 describe "#set_description" do
180 attr_reader :example_group
182 class << example_group
183 public :set_description
187 describe "#set_description(String)" do
189 example_group.set_description("abc")
192 specify ".description should return the String passed into .set_description" do
193 example_group.description.should == "abc"
196 specify ".described_type should provide nil as its type" do
197 example_group.described_type.should be_nil
201 describe "#set_description(Type)" do
203 example_group.set_description(ExampleGroup)
206 specify ".description should return a String representation of that type (fully qualified) as its name" do
207 example_group.description.should == "Spec::Example::ExampleGroup"
210 specify ".described_type should return the passed in type" do
211 example_group.described_type.should == Spec::Example::ExampleGroup
215 describe "#set_description(String, Type)" do
217 example_group.set_description("behaving", ExampleGroup)
220 specify ".description should return String then space then Type" do
221 example_group.description.should == "behaving Spec::Example::ExampleGroup"
224 specify ".described_type should return the passed in type" do
225 example_group.described_type.should == Spec::Example::ExampleGroup
229 describe "#set_description(Type, String not starting with a space)" do
231 example_group.set_description(ExampleGroup, "behaving")
234 specify ".description should return the Type then space then String" do
235 example_group.description.should == "Spec::Example::ExampleGroup behaving"
239 describe "#set_description(Type, String starting with .)" do
241 example_group.set_description(ExampleGroup, ".behaving")
244 specify ".description should return the Type then String" do
245 example_group.description.should == "Spec::Example::ExampleGroup.behaving"
249 describe "#set_description(Type, String containing .)" do
251 example_group.set_description(ExampleGroup, "calling a.b")
254 specify ".description should return the Type then space then String" do
255 example_group.description.should == "Spec::Example::ExampleGroup calling a.b"
259 describe "#set_description(Type, String starting with .)" do
261 example_group.set_description(ExampleGroup, ".behaving")
264 specify "should return the Type then String" do
265 example_group.description.should == "Spec::Example::ExampleGroup.behaving"
269 describe "#set_description(Type, String containing .)" do
271 example_group.set_description(ExampleGroup, "is #1")
274 specify ".description should return the Type then space then String" do
275 example_group.description.should == "Spec::Example::ExampleGroup is #1"
279 describe "#set_description(String, Type, String)" do
281 example_group.set_description("A", Hash, "with one entry")
284 specify ".description should return the first String then space then Type then second String" do
285 example_group.description.should == "A Hash with one entry"
289 describe "#set_description(Hash representing options)" do
291 example_group.set_description(:a => "b", :spec_path => "blah")
294 it ".spec_path should expand the passed in :spec_path option passed into the constructor" do
295 example_group.spec_path.should == File.expand_path("blah")
298 it ".description_options should return all the options passed in" do
299 example_group.description_options.should == {:a => "b", :spec_path => "blah"}
305 describe "#description" do
306 it "should return the same description instance for each call" do
307 example_group.description.should eql(example_group.description)
310 it "should not add a space when description_text begins with #" do
311 child_example_group = Class.new(example_group) do
312 describe("#foobar", "Does something")
314 child_example_group.description.should == "ExampleGroup#foobar Does something"
317 it "should not add a space when description_text begins with ." do
318 child_example_group = Class.new(example_group) do
319 describe(".foobar", "Does something")
321 child_example_group.description.should == "ExampleGroup.foobar Does something"
324 it "should return the class name if nil" do
325 example_group.set_description(nil)
326 example_group.description.should =~ /Class:/
329 it "should return the class name if nil" do
330 example_group.set_description("")
331 example_group.description.should =~ /Class:/
335 describe "#description_parts" do
336 it "should return an Array of the current class description args" do
337 example_group.description_parts.should == [example_group.description]
340 it "should return an Array of the description args from each class in the hierarchy" do
341 child_example_group = Class.new(example_group)
342 child_example_group.describe("Child", ExampleGroup)
343 child_example_group.description.should_not be_empty
345 grand_child_example_group = Class.new(child_example_group)
346 grand_child_example_group.describe("GrandChild", ExampleGroup)
347 grand_child_example_group.description.should_not be_empty
349 grand_child_example_group.description_parts.should == [
352 Spec::Example::ExampleGroup,
354 Spec::Example::ExampleGroup
359 describe "#described_type" do
360 it "should return passed in type" do
361 child_example_group = Class.new(example_group) do
364 child_example_group.described_type.should == Object
367 it "should return #described_type of superclass when no passed in type" do
368 parent_example_group = Class.new(ExampleGroup) do
369 describe Object, "#foobar"
371 child_example_group = Class.new(parent_example_group) do
372 describe "not a type"
374 child_example_group.described_type.should == Object
378 describe "#remove_after" do
379 it "should unregister a given after(:each) block" do
380 after_all_ran = false
381 @example_group.it("example") {}
382 proc = Proc.new { after_all_ran = true }
383 ExampleGroup.after(:each, &proc)
385 after_all_ran.should be_true
387 after_all_ran = false
388 ExampleGroup.remove_after(:each, &proc)
390 after_all_ran.should be_false
394 describe "#include" do
395 it "should have accessible class methods from included module" do
396 mod1_method_called = false
398 class_methods = Module.new do
399 define_method :mod1_method do
400 mod1_method_called = true
404 metaclass.class_eval do
405 define_method(:included) do |receiver|
406 receiver.extend class_methods
411 mod2_method_called = false
413 class_methods = Module.new do
414 define_method :mod2_method do
415 mod2_method_called = true
419 metaclass.class_eval do
420 define_method(:included) do |receiver|
421 receiver.extend class_methods
426 @example_group.include mod1, mod2
428 @example_group.mod1_method
429 @example_group.mod2_method
430 mod1_method_called.should be_true
431 mod2_method_called.should be_true
435 describe "#number_of_examples" do
436 it "should count number of specs" do
438 @example_group.it("one") {}
439 @example_group.it("two") {}
440 @example_group.it("three") {}
441 @example_group.it("four") {}
442 end.should change {@example_group.number_of_examples}.by(4)
446 describe "#class_eval" do
447 it "should allow constants to be defined" do
448 example_group = Class.new(ExampleGroup) do
451 it "should reference FOO" do
456 Object.const_defined?(:FOO).should == false
460 describe '#register' do
461 it "should add ExampleGroup to set of ExampleGroups to be run" do
462 options.example_groups.delete(example_group)
463 options.example_groups.should_not include(example_group)
465 example_group.register {}
466 options.example_groups.should include(example_group)
470 describe '#unregister' do
472 options.example_groups.should include(example_group)
475 it "should remove ExampleGroup from set of ExampleGroups to be run" do
476 example_group.unregister
477 options.example_groups.should_not include(example_group)
481 describe "#registration_backtrace" do
482 it "returns the backtrace of where the ExampleGroup was registered" do
483 example_group = Class.new(ExampleGroup)
484 example_group.registration_backtrace.join("\n").should include("#{__FILE__}:#{__LINE__-1}")