-
Notifications
You must be signed in to change notification settings - Fork 27
Scope (where everything is executed)
brianc edited this page Aug 12, 2010
·
2 revisions
When you want to make custom assertion macros or do more complicated things within your tests it’s helpful to understand where everything is executed. Your assertions and setup are run inside of a special ‘jail’ object — Riot::Situation. Every context has its own Riot::Situation instance. Nested contexts do not share the situation with their parent; however, their parents setups are all run on the child’s Riot::Situation instance before the child context’s setups are run. Within your assertions you cannot access any instance variables within the body of the context, and your assertions have no way to access the context in which they are being executed.
For example:
context "My context" do
#this is the context scope
#it is not accessible from the assertions or the setup
@instance_var = 1
setup do
#this is executed within a new Riot::Situation instance
@instance_var #=> nil
@instance_var = 2
end
#@instance_var #=> 1
asserts("is executed in scope of setup") { @instance_var }.equals(2) #=> passes
context "nested context" do
# a nested context has no knowledge or access to its parent's scope
@instance_var #=> nil
@instance_var = "yo"
setup do
#this is executed within a new Riot::Situation instance
#AFTER all parent setups are re-executed within the new Riot::Situation instance
@instance_var #=> 2
@instance_var = 3
end
asserts("instance var from parent has been incremented") { @instance_var }.equals(3) #=> passes
end #nested context
asserts("nested context did not modify instance var") { @instance_var }.equals(2) #=> passes
end