0
@@ -8,13 +8,13 @@ module Thoughtbot
0
+ # =
= Should statements
0
# Should statements are just syntactic sugar over normal Test::Unit test methods. A should block
0
# contains all the normal code and assertions you're used to seeing, with the added benefit that
0
# they can be wrapped inside context blocks (see below).
0
# class UserTest << Test::Unit::TestCase
0
@@ -32,21 +32,35 @@ module Thoughtbot
0
# * <tt>"test: User should return its full name. "</tt>
0
# Note: The part before <tt>should</tt> in the test name is gleamed from the name of the Test::Unit class.
0
+ # Should statements can also take a Proc as a <tt>:before </tt>option. This proc runs after any
0
+ # parent context's setups but before the current context's setup.
0
+ # context "Some context" do
0
+ # setup { puts("I run after the :before proc") }
0
+ # should "run a :before proc", :before => lambda { puts("I run before the setup") } do
0
- def should(name,
&blk)
0
+ def should(name,
options = {}, &blk)
0
if Shoulda.current_context
0
- block_given? ? Shoulda.current_context.should(name,
&blk) : Should.current_context.should_eventually(name)
0
+ block_given? ? Shoulda.current_context.should(name,
options, &blk) : Should.current_context.should_eventually(name)
0
context_name = self.name.gsub(/Test/, "")
0
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
0
- block_given? ? should(name,
&blk) : should_eventually(name)
0
+ block_given? ? should(name,
options, &blk) : should_eventually(name)
0
# Just like should, but never runs, and instead prints an 'X' in the Test::Unit output.
0
- def should_eventually(name,
&blk)
0
+ def should_eventually(name,
options = {}, &blk)
0
context_name = self.name.gsub(/Test/, "")
0
context = Thoughtbot::Shoulda::Context.new(context_name, self) do
0
should_eventually(name, &blk)
0
@@ -54,7 +68,7 @@ module Thoughtbot
0
# A context block groups should statements under a common set of setup/teardown methods.
0
# Context blocks can be arbitrarily nested, and can do wonders for improving the maintainability
0
@@ -154,9 +168,9 @@ module Thoughtbot
0
self.teardown_blocks << blk
0
- def should(name,
&blk)
0
+ def should(name,
options = {}, &blk)
0
- self.shoulds << { :name => name, :b
lock => blk }
0
+ self.shoulds << { :name => name, :b
efore => options[:before], :block => blk }
0
self.should_eventuallys << { :name => name }
0
@@ -189,7 +203,9 @@ module Thoughtbot
0
test_unit_class.send(:define_method, test_name) do
0
- context.run_all_setup_blocks(self)
0
+ context.run_parent_setup_blocks(self)
0
+ should[:before].bind(self).call if should[:before]
0
+ context.run_current_setup_blocks(self)
0
should[:block].bind(self).call
0
context.run_all_teardown_blocks(self)
0
@@ -198,7 +214,15 @@ module Thoughtbot
0
def run_all_setup_blocks(binding)
0
+ run_parent_setup_blocks(binding)
0
+ run_current_setup_blocks(binding)
0
+ def run_parent_setup_blocks(binding)
0
self.parent.run_all_setup_blocks(binding) if am_subcontext?
0
+ def run_current_setup_blocks(binding)
0
setup_blocks.each do |setup_block|
0
setup_block.bind(binding).call