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
142 143 144 145 146 147 148 |
# File 'lib/nutrasuite/contexts.rb', line 142 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.
130 131 132 |
# File 'lib/nutrasuite/contexts.rb', line 130 def name @name end |
#setups ⇒ Object (readonly)
Internal: Get the name, setup methods, and teardown methods for this Context.
130 131 132 |
# File 'lib/nutrasuite/contexts.rb', line 130 def setups @setups end |
#teardowns ⇒ Object (readonly)
Internal: Get the name, setup methods, and teardown methods for this Context.
130 131 132 |
# File 'lib/nutrasuite/contexts.rb', line 130 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.
169 170 171 172 173 174 175 176 177 |
# File 'lib/nutrasuite/contexts.rb', line 169 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.
199 200 201 |
# File 'lib/nutrasuite/contexts.rb', line 199 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.
207 208 209 |
# File 'lib/nutrasuite/contexts.rb', line 207 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.
186 187 188 189 190 191 192 193 |
# File 'lib/nutrasuite/contexts.rb', line 186 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.
153 154 155 |
# File 'lib/nutrasuite/contexts.rb', line 153 def build @block.call end |