Skip to content
Browse files

initial stab at new SharedContext feature

- replaces rspec-1's share_as
  • Loading branch information...
1 parent 4e852f1 commit fdaf38a606234defa664d10dae79bb4e47e0c25f @dchelimsky dchelimsky committed
View
53 features/example_groups/shared_context.feature
@@ -0,0 +1,53 @@
+Feature: shared context
+
+ Common setup, teardown, helper methods, etc, can be shared across example
+ groups using a SharedContext: a Ruby Module that extends RSpec::SharedContext
+
+ Scenario: share a helper method
+ Given a file named "example_spec.rb" with:
+ """
+ module Helpers
+ extend RSpec::SharedContext
+
+ def helper_method
+ "text from the helper method"
+ end
+ end
+
+ describe "something" do
+ include Helpers
+
+ it "does something" do
+ helper_method.should match(/^text from/)
+ end
+ end
+ """
+ When I run "rspec example_spec.rb"
+ Then the examples should all pass
+
+ Scenario: share before and after hooks
+ Given a file named "example_spec.rb" with:
+ """
+ module Shared
+ extend RSpec::SharedContext
+
+ before(:all) { puts "before all" }
+ before(:each) { puts "before each" }
+ after(:each) { puts "after each" }
+ after(:all) { puts "after all" }
+ end
+
+ describe "something" do
+ include Shared
+ example { puts "example" }
+ end
+ """
+ When I run "rspec example_spec.rb"
+ And the output should contain:
+ """
+ before all
+ before each
+ example
+ after each
+ .after all
+ """
View
5 features/step_definitions/additional_cli_steps.rb
@@ -9,3 +9,8 @@
combined_output.should_not =~ compile_and_escape(string)
end
end
+
+Then /^the example(s)? should( all)? pass$/ do |*|
+ Then %q{the output should contain "0 failures"}
+ Then %q{the exit status should be 0}
+end
View
3 lib/rspec/core.rb
@@ -19,6 +19,7 @@
require 'rspec/core/drb_command_line'
require 'rspec/core/runner'
require 'rspec/core/example'
+require 'rspec/core/shared_context'
require 'rspec/core/shared_example_group'
require 'rspec/core/example_group'
require 'rspec/core/version'
@@ -27,6 +28,8 @@
module RSpec
autoload :Matchers, 'rspec/matchers'
+ SharedContext = Core::SharedContext
+
module Core
def self.install_directory
@install_directory ||= File.expand_path(File.dirname(__FILE__))
View
16 lib/rspec/core/shared_context.rb
@@ -0,0 +1,16 @@
+module RSpec
+ module Core
+ module SharedContext
+ include Hooks
+
+ def included(group)
+ [:before, :after].each do |type|
+ [:all, :each].each do |scope|
+ group.hooks[type][scope].concat hooks[type][scope]
+ end
+ end
+ end
+
+ end
+ end
+end
View
3 lib/rspec/core/shared_example_group.rb
@@ -7,6 +7,8 @@ def share_examples_for(name, &block)
RSpec.world.shared_example_groups[name] = block
end
+ alias :shared_examples_for :share_examples_for
+
def share_as(name, &block)
if Object.const_defined?(name)
mod = Object.const_get(name)
@@ -31,7 +33,6 @@ def self.included(kls)
RSpec.world.shared_example_groups[shared_const] = block
end
- alias :shared_examples_for :share_examples_for
private
View
30 spec/rspec/core/shared_context_spec.rb
@@ -0,0 +1,30 @@
+require "spec_helper"
+
+describe RSpec::Core::SharedContext do
+ describe "hooks" do
+ it "creates a before hook" do
+ before_all_hook = false
+ before_each_hook = false
+ after_each_hook = false
+ after_all_hook = false
+ shared = Module.new do
+ extend RSpec::Core::SharedContext
+ before(:all) { before_all_hook = true }
+ before(:each) { before_each_hook = true }
+ after(:each) { after_each_hook = true }
+ after(:all) { after_all_hook = true }
+ end
+ group = RSpec::Core::ExampleGroup.describe do
+ include shared
+ example { }
+ end
+
+ group.run
+
+ before_all_hook.should be_true
+ before_each_hook.should be_true
+ after_each_hook.should be_true
+ after_all_hook.should be_true
+ end
+ end
+end

0 comments on commit fdaf38a

Please sign in to comment.
Something went wrong with that request. Please try again.