Permalink
Browse files

raise when shared example group doesn't exist

  • Loading branch information...
1 parent 82bf93e commit e4083fd87983ab918ba64e2fa61954da4a8beb76 @dchelimsky dchelimsky committed Mar 14, 2010
Showing with 62 additions and 16 deletions.
  1. +6 −2 lib/rspec/core/example_group.rb
  2. +56 −14 spec/rspec/core/shared_example_group_spec.rb
@@ -46,8 +46,12 @@ def self.#{new_alias}(desc=nil, options={}, &block)
alias_example_to :pending, :pending => true
def self.it_should_behave_like(*names)
- Rspec::Core.world.shared_example_groups.each do |name, block|
- module_eval(&block) if names.include?(name)
+ names.each do |name|
+ begin
+ module_eval &Rspec::Core.world.shared_example_groups[name]
+ rescue ArgumentError
+ raise "Could not find shared example group named #{name.inspect}"
+ end
end
end
@@ -65,7 +65,14 @@ def extra_helper; end
group.singleton_methods.should include(:class_helper)
end
- it "should raise when named shared example_group can not be found"
+ it "raises when named shared example_group can not be found" do
+ cleanup_shared_example_groups do
+ group = ExampleGroup.create("example_group")
+ lambda do
+ group.it_should_behave_like("a group that does not exist")
+ end.should raise_error(/Could not find shared example group named/)
+ end
+ end
it "adds examples to current example_group using it_should_behave_like" do
cleanup_shared_example_groups do
@@ -142,35 +149,70 @@ module RunningSharedExamplesJustForTesting; end
share_examples_for("it runs shared examples") do
include RunningSharedExamplesJustForTesting
+ class << self
+ def magic
+ $magic ||= {}
+ end
+
+ def count(scope)
+ @counters ||= {
+ :before_all => 0,
+ :before_each => 0,
+ :after_each => 0,
+ :after_all => 0
+ }
+ @counters[scope] += 1
+ end
+ end
+
def magic
- @magic ||= {}
+ $magic ||= {}
+ end
+
+ def count(scope)
+ self.class.count(scope)
end
- before(:each) { magic[:before_each] = 'each' }
- after(:each) { magic[:after_each] = 'each' }
- before(:all) { magic[:before_all] = 'all' }
+ before(:all) { magic[:before_all] = "before all #{count(:before_all)}" }
+ before(:each) { magic[:before_each] = "before each #{count(:before_each)}" }
+ after(:each) { magic[:after_each] = "after each #{count(:after_each)}" }
+ after(:all) { magic[:after_all] = "after all #{count(:after_all)}" }
end
- it_should_behave_like "it runs shared examples"
+ let(:group) do
+ group = ExampleGroup.create("example group") do
+ it_should_behave_like "it runs shared examples"
+ it "has one example" do; end
+ it "has another example" do; end
+ end
+ end
+
+ before { group.run(stub('reporter').as_null_object) }
+
+ it "runs before(:all) only once from shared example_group", :compat => 'rspec-1.2' do
+ group.magic[:before_all].should eq("before all 1")
+ end
it "runs before(:each) from shared example_group", :compat => 'rspec-1.2' do
- magic[:before_each].should == 'each'
+ group.magic[:before_each].should eq("before each 2")
end
- it "runs after(:each) from shared example_group", :compat => 'rspec-1.2'
+ it "runs after(:each) from shared example_group", :compat => 'rspec-1.2' do
+ group.magic[:after_each].should eq("after each 2")
+ end
- it "should run before(:all) only once from shared example_group", :compat => 'rspec-1.2' do
- magic[:before_all].should == 'all'
+ it "runs after(:all) only once from shared example_group", :compat => 'rspec-1.2' do
+ group.magic[:after_all].should eq("after all 1")
end
- it "should run after(:all) only once from shared example_group", :compat => 'rspec-1.2'
+ it_should_behave_like "it runs shared examples"
- it "should include modules, included into shared example_group, into current example_group", :compat => 'rspec-1.2' do
+ it "includes modules, included into shared example_group, into current example_group", :compat => 'rspec-1.2' do
running_example.example_group.included_modules.should include(RunningSharedExamplesJustForTesting)
end
- it "should make methods defined in the shared example_group available in consuming example_group", :compat => 'rspec-1.2' do
- magic.should be_a(Hash)
+ it "makes methods defined in the shared example_group available in consuming example_group", :compat => 'rspec-1.2' do
+ group.magic.should be_a(Hash)
end
end

0 comments on commit e4083fd

Please sign in to comment.