Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow any object in the shared spec description.

  • Loading branch information...
commit 2bae7a4cef5fd48166833da3f3dc35eb1c6b4584 1 parent d848efe
Manfred Stienstra Manfred authored
Showing with 48 additions and 8 deletions.
  1. +24 −8 lib/test/spec/share.rb
  2. +24 −0 test/share_test.rb
32 lib/test/spec/share.rb
View
@@ -1,7 +1,11 @@
$shared_specs = {}
-module Kernel
- # Stores the passed in block for inclusion in test cases.
+module SharedSpecsInclusionHelper
+ def self.description(*parts)
+ parts.map(&:to_s).join(' ')
+ end
+
+ # Include the specified shared specs in this test case.
#
# share "User" do
# it "should authenticate" do
@@ -18,13 +22,13 @@ module Kernel
# end
#
# 2 tests, 2 assertions, 0 failures, 0 errors
- def share(name, &specs_block)
- $shared_specs[name] = specs_block
+ def shared_specs_for(*description)
+ self.class_eval &$shared_specs[SharedSpecsInclusionHelper.description(*description)]
end
end
-module SharedSpecsInclusionHelper
- # Include the specified shared specs in this test case.
+module Kernel
+ # Stores the passed in block for inclusion in test cases.
#
# share "User" do
# it "should authenticate" do
@@ -41,8 +45,20 @@ module SharedSpecsInclusionHelper
# end
#
# 2 tests, 2 assertions, 0 failures, 0 errors
- def shared_specs_for(name)
- self.class_eval &$shared_specs[name]
+ #
+ # You can use anything as a description for the shared specs.
+ #
+ # share User, "concerning the use of", 1, :email, "symbol" do
+ # it "should process" do
+ # end
+ # end
+ #
+ # describe User do
+ # shared_specs_for User, "concerning the use of", 1, :email, "symbol"
+ # end
+ #
+ def share(*description, &specs_block)
+ $shared_specs[SharedSpecsInclusionHelper.description(*description)] = specs_block
end
end
24 test/share_test.rb
View
@@ -10,6 +10,10 @@ def it(name, &block)
@times_called ||= 0
@times_called += 1
end
+
+ def reset!
+ @times_called = nil
+ end
end
end
@@ -25,6 +29,10 @@ def it(name, &block)
end
describe "Kernel#share" do
+ before do
+ DummyMock.reset!
+ end
+
it "should add the shared specs to the global shared modules variable" do
before = $shared_specs.length
share("Bar") {}
@@ -34,9 +42,25 @@ def it(name, &block)
it "should have stored the proc that holds the specs" do
$shared_specs['Dummy'].should.be.instance_of Proc
end
+
+ it "accepts mixed objects as a description" do
+ before = $shared_specs.length
+ share(Kernel, "concerning modules", 1) {
+ it("works") {}
+ }
+ $shared_specs.length.should == before + 1
+ DummyMock.class_eval do
+ shared_specs_for Kernel, "concerning modules", 1
+ end
+ DummyMock.times_called.should.be 1
+ end
end
describe "SharedSpecsInclusionHelper::shared_specs_for" do
+ before do
+ DummyMock.reset!
+ end
+
it "should have extended Test::Unit::TestCase" do
Test::Unit::TestCase.should.respond_to :shared_specs_for
end
Please sign in to comment.
Something went wrong with that request. Please try again.