Class: Nutrasuite::Context
- Inherits:
-
Object
- Object
- Nutrasuite::Context
- Defined in:
- lib/nutrasuite/contexts.rb
Overview
Internal: The Context class represents each context that can go on the context stack. Each context is responsible for tracking its specific information (mostly its name and its setup and teardown methods). Contexts should only be created using one of the article methods in ContextHelpers.
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Internal: Get the name, setup methods, and teardown methods for this Context.
-
#setups ⇒ Object
readonly
Internal: Get the name, setup methods, and teardown methods for this Context.
-
#teardowns ⇒ Object
readonly
Internal: Get the name, setup methods, and teardown methods for this Context.
Class Method Summary collapse
-
.build_test_name(name = "") ⇒ Object
Internal: builds a test name based on the current state of the context stack.
-
.context_stack ⇒ Object
Internal: get the context stack, or initialize it to an empty list if necessary.
-
.current_context ⇒ Object
Internal: get the current context.
-
.push(name, &block) ⇒ Object
Internal: pushes a new context onto the stack, builds that context out, and then removes it from the stack.
Instance Method Summary collapse
-
#build ⇒ Object
Internal: build runs the block that defines the context’s contents, thus setting up any nested contexts and tests.
-
#initialize(name, &block) ⇒ Context
constructor
Internal: Create a new Context object.
Constructor Details
#initialize(name, &block) ⇒ Context
Internal: Create a new Context object.
name - The name of the context, which will be prepended to any nested
contexts/tests in the final test name
&block - The block that defines the contents of the context; should
consist of:
- test definitions
- sub-context definitions
- setup and teardown declarations
141 142 143 144 145 146 147 |
# File 'lib/nutrasuite/contexts.rb', line 141 def initialize(name, &block) @name = name @block = block @setups = [] @teardowns = [] end |
Instance Attribute Details
#name ⇒ Object (readonly)
Internal: Get the name, setup methods, and teardown methods for this Context.
129 130 131 |
# File 'lib/nutrasuite/contexts.rb', line 129 def name @name end |
#setups ⇒ Object (readonly)
Internal: Get the name, setup methods, and teardown methods for this Context.
129 130 131 |
# File 'lib/nutrasuite/contexts.rb', line 129 def setups @setups end |
#teardowns ⇒ Object (readonly)
Internal: Get the name, setup methods, and teardown methods for this Context.
129 130 131 |
# File 'lib/nutrasuite/contexts.rb', line 129 def teardowns @teardowns end |
Class Method Details
.build_test_name(name = "") ⇒ Object
Internal: builds a test name based on the current state of the context stack.
Examples
# Assuming the context stack looks like "a user","that is an admin":
Context.build_test_name("has admin privileges")
#=> "test a user that is an admin has admin privileges"
Returns a string name for a method that will automatically be executed by MiniTest.
168 169 170 171 172 173 174 175 176 |
# File 'lib/nutrasuite/contexts.rb', line 168 def self.build_test_name(name="") full_name = "test " context_stack.each do |context| unless context.name.nil? full_name << context.name << " " end end full_name << name end |
.context_stack ⇒ Object
Internal: get the context stack, or initialize it to an empty list if necessary.
Returns: an Array representing the context stack.
198 199 200 |
# File 'lib/nutrasuite/contexts.rb', line 198 def self.context_stack @context_stack ||= [] end |
.current_context ⇒ Object
Internal: get the current context.
Returns: the context currently at the top of the stack, or nil if there are no contexts in the stack at the moment.
206 207 208 |
# File 'lib/nutrasuite/contexts.rb', line 206 def self.current_context context_stack.last end |
.push(name, &block) ⇒ Object
Internal: pushes a new context onto the stack, builds that context out, and then removes it from the stack.
This method should be the only means by which contexts get built, as it ensures that the context is removed from the context stack when it’s done building itself.
185 186 187 188 189 190 191 192 |
# File 'lib/nutrasuite/contexts.rb', line 185 def self.push(name, &block) context = Context.new(name, &block) context_stack.push(context) context.build context_stack.pop end |
Instance Method Details
#build ⇒ Object
Internal: build runs the block that defines the context’s contents, thus setting up any nested contexts and tests.
152 153 154 |
# File 'lib/nutrasuite/contexts.rb', line 152 def build @block.call end |