lib/shoulda/context/context.rb in shoulda-context-1.0.2 vs lib/shoulda/context/context.rb in shoulda-context-1.1.0
- old
+ new
@@ -1,12 +1,12 @@
module Shoulda
module Context
class << self
- attr_accessor :contexts
def contexts # :nodoc:
@contexts ||= []
end
+ attr_writer :contexts
def current_context # :nodoc:
self.contexts.last
end
@@ -224,11 +224,11 @@
def subject(&block)
@subject_block = block
end
def subject_block # :nodoc:
- @subject_block
+ @subject_block ||= nil
end
end
module InstanceMethods
# Returns an instance of the class under test.
@@ -288,21 +288,28 @@
attr_accessor :subcontexts # array of contexts nested under myself
attr_accessor :setup_blocks # blocks given via setup methods
attr_accessor :teardown_blocks # blocks given via teardown methods
attr_accessor :shoulds # array of hashes representing the should statements
attr_accessor :should_eventuallys # array of hashes representing the should eventually statements
- attr_accessor :subject_block
+ # accessor with cache
+ def subject_block
+ return @subject_block if @subject_block
+ parent.subject_block
+ end
+ attr_writer :subject_block
+
def initialize(name, parent, &blk)
Shoulda::Context.add_context(self)
self.name = name
self.parent = parent
self.setup_blocks = []
self.teardown_blocks = []
self.shoulds = []
self.should_eventuallys = []
self.subcontexts = []
+ self.subject_block = nil
if block_given?
merge_block(&blk)
else
merge_block { warn " * WARNING: Block missing for context '#{full_name}'" }
@@ -335,12 +342,12 @@
end
if blk
self.shoulds << { :name => name, :before => options[:before], :block => blk }
else
- self.should_eventuallys << { :name => name }
- end
+ self.should_eventuallys << { :name => name }
+ end
end
def should_not(matcher)
name = matcher.description
blk = lambda { assert_rejects matcher, subject }
@@ -351,14 +358,9 @@
self.should_eventuallys << { :name => name, :block => blk }
end
def subject(&block)
self.subject_block = block
- end
-
- def subject_block
- return @subject_block if @subject_block
- parent.subject_block
end
def full_name
parent_name = parent.full_name if am_subcontext?
return [parent_name, name].join(" ").strip