mirror of
https://github.com/thoughtbot/shoulda-matchers.git
synced 2022-11-09 12:01:38 -05:00
- fixed bugs with nested contexts
git-svn-id: https://svn.thoughtbot.com/plugins/tb_test_helpers/trunk@44 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
This commit is contained in:
parent
6337846ab4
commit
a8a759ae59
4 changed files with 32 additions and 30 deletions
|
@ -1,5 +1,5 @@
|
|||
class Test # :nodoc:
|
||||
class Unit # :nodoc:
|
||||
module Test # :nodoc:
|
||||
module Unit # :nodoc:
|
||||
class TestCase
|
||||
class << self
|
||||
# Ensures that the model cannot be saved if one of the attributes listed is not present.
|
||||
|
|
|
@ -1,56 +1,58 @@
|
|||
module TBTestHelpers # :nodoc:
|
||||
module Should
|
||||
def Should.included(other) # :nodoc:
|
||||
@@_context_names = []
|
||||
@@_setup_blocks = []
|
||||
@@_teardown_blocks = []
|
||||
@@context_names = []
|
||||
@@setup_blocks = []
|
||||
@@teardown_blocks = []
|
||||
end
|
||||
|
||||
# Creates a context block with the given name. The context block can contain setup, should, should_eventually, and teardown blocks.
|
||||
def context(name, &context_block)
|
||||
@@_context_names << name
|
||||
saved_setups = @@setup_blocks.dup
|
||||
saved_teardowns = @@teardown_blocks.dup
|
||||
saved_contexts = @@context_names.dup
|
||||
|
||||
@@context_names << name
|
||||
context_block.bind(self).call
|
||||
@@_context_names.pop
|
||||
@@_setup_blocks.pop
|
||||
@@_teardown_blocks.pop
|
||||
|
||||
@@context_names = saved_contexts
|
||||
@@setup_blocks = saved_setups
|
||||
@@teardown_blocks = saved_teardowns
|
||||
end
|
||||
|
||||
# Run before every should block in the current context
|
||||
def setup(&setup_block)
|
||||
@@_setup_blocks << setup_block
|
||||
@@setup_blocks << setup_block
|
||||
end
|
||||
|
||||
# Run after every should block in the current context
|
||||
def teardown(&teardown_block)
|
||||
@@_teardown_blocks << teardown_block
|
||||
@@teardown_blocks << teardown_block
|
||||
end
|
||||
|
||||
# Defines a specification. Can be called either inside our outside of a context.
|
||||
def should(name, opts = {}, &should_block)
|
||||
unless @@_context_names.empty?
|
||||
test_name = "test #{@@_context_names.join(" ")} should #{name}"
|
||||
else
|
||||
test_name = "test should #{name}"
|
||||
end
|
||||
test_name_sym = test_name.to_sym
|
||||
|
||||
test_name = ["test", @@context_names, "should", "#{name}"].flatten.join(' ').to_sym
|
||||
|
||||
raise ArgumentError, "'#{test_name}' is already defined" and return if self.instance_methods.include? test_name
|
||||
|
||||
setup_block = @@_setup_blocks.last
|
||||
teardown_block = @@_teardown_blocks.last
|
||||
setup_blocks = @@setup_blocks.dup
|
||||
teardown_blocks = @@teardown_blocks.dup
|
||||
|
||||
if opts[:unimplemented]
|
||||
define_method test_name_sym do |*args|
|
||||
define_method test_name do |*args|
|
||||
# XXX find a better way of doing this.
|
||||
assert true
|
||||
STDOUT.putc "X" # Tests for this model are missing.
|
||||
end
|
||||
else
|
||||
define_method test_name_sym do |*args|
|
||||
setup_block.bind(self).call if setup_block
|
||||
should_block.bind(self).call(*args)
|
||||
ensure
|
||||
teardown_block.bind(self).call if teardown_block
|
||||
define_method test_name do |*args|
|
||||
begin
|
||||
setup_blocks.each {|b| b.bind(self).call }
|
||||
should_block.bind(self).call(*args)
|
||||
ensure
|
||||
teardown_blocks.reverse.each {|b| b.bind(self).call }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,8 +1,8 @@
|
|||
require 'active_record_helpers'
|
||||
require 'should'
|
||||
|
||||
class Test # :nodoc:
|
||||
class Unit # :nodoc:
|
||||
module Test # :nodoc:
|
||||
module Unit # :nodoc:
|
||||
class TestCase
|
||||
class << self
|
||||
include TBTestHelpers::Should
|
||||
|
|
|
@ -15,13 +15,13 @@ class ContextTest < Test::Unit::TestCase # :nodoc:
|
|||
assert_match(/^test context with setup block/, self.to_s)
|
||||
end
|
||||
|
||||
context "and with a subcontext" do
|
||||
context "and a subcontext" do
|
||||
setup do
|
||||
@blah = "#{@blah} twice"
|
||||
end
|
||||
|
||||
should "be named correctly" do
|
||||
assert_match(/^test context with subcontext should be named correctly/, self.to_s)
|
||||
assert_match(/^test context with setup block and a subcontext should be named correctly/, self.to_s)
|
||||
end
|
||||
|
||||
should "run the setup methods in order" do
|
||||
|
|
Loading…
Add table
Reference in a new issue