Module: RspecInContext::InContext::ClassMethods
- Defined in:
- lib/rspec_in_context/in_context.rb
Overview
This module define the methods that will be available for the end user inside RSpec tests
Instance Method Summary collapse
-
#define_context(context_name, namespace: nil, ns: nil, silent: true, print_context: nil) ⇒ Object
Let you define a context that can be reused later.
-
#execute_tests ⇒ Object
(also: #instantiate_context)
Used in context definition Place where you want to re-inject code passed in argument of in_context (for more examples look at tests).
-
#in_context(context_name, *args, namespace: nil, ns: nil, &block) ⇒ Object
Use a context and inject its content at this place in the code.
-
#instanciate_context ⇒ Object
deprecated
Deprecated.
Use #instantiate_context or #execute_tests instead
Instance Method Details
#define_context(context_name, namespace: nil, ns: nil, silent: true, print_context: nil) ⇒ Object
Note:
contexts are scoped to the block they are defined in.
Let you define a context that can be reused later
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
# File 'lib/rspec_in_context/in_context.rb', line 207 def define_context( context_name, namespace: nil, ns: nil, silent: true, print_context: nil, & ) namespace ||= ns silent = !print_context unless print_context.nil? InContext.add_context( context_name, hooks.instance_variable_get(:@owner), namespace, silent, & ) end |
#execute_tests ⇒ Object Also known as: instantiate_context
Used in context definition Place where you want to re-inject code passed in argument of in_context (for more examples look at tests)
180 181 182 183 |
# File 'lib/rspec_in_context/in_context.rb', line 180 def execute_tests current_block = Thread.current[:test_block_stack]&.last instance_exec(¤t_block) if current_block end |
#in_context(context_name, *args, namespace: nil, ns: nil, &block) ⇒ Object
Use a context and inject its content at this place in the code
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/rspec_in_context/in_context.rb', line 157 def in_context(context_name, *args, namespace: nil, ns: nil, &block) namespace ||= ns context_to_exec = InContext.find_context(context_name, namespace) Thread.current[:test_block_stack] ||= [] Thread.current[:test_block_stack].push(block) begin if context_to_exec.silent context("", rspec_in_context_silent: true) do instance_exec(*args, &context_to_exec.block) end else context( context_name.to_s, ) { instance_exec(*args, &context_to_exec.block) } end ensure Thread.current[:test_block_stack].pop end end |
#instanciate_context ⇒ Object
Deprecated.
Use #instantiate_context or #execute_tests instead
187 188 189 190 191 192 193 194 |
# File 'lib/rspec_in_context/in_context.rb', line 187 def instanciate_context warn( "DEPRECATION: `instanciate_context` is deprecated due to a typo. " \ "Use `instantiate_context` or `execute_tests` instead.", uplevel: 1, ) execute_tests end |