module RspecInContext::InContext::ClassMethods
This module define the methods that will be available for the end user inside RSpec
tests
Public Instance Methods
Let you define a context that can be reused later
@param context_name [String, Symbol] The name of the context that will be re-used later @param namespace [String, Symbol] namespace name where the context will be stored.
It helps reducing colisions when you define "global" contexts
@param ns [String, Symbol] Alias of namespace @param block [Proc] Contain the code that will be injected with in_context
later @param silent [Boolean] Does the in_context
wrap itself into a context with its name or an anonymous context @param print_context [Boolean] Reverse alias of silent
@note contexts are scoped to the block they are defined in.
# File lib/rspec_in_context/in_context.rb, line 114 def define_context(context_name, namespace: nil, ns: nil, silent: true, print_context: nil, &block) namespace ||= ns silent = print_context.nil? ? silent : !print_context instance_exec do InContext.add_context(context_name, hooks.instance_variable_get(:@owner), namespace, silent, &block) end end
Used in context definition Place where you want to re-inject code passed in argument of in_context
For convenience and readability, a `instanciate_context` alias have been defined (for more examples look at tests)
# File lib/rspec_in_context/in_context.rb, line 98 def execute_tests instance_exec(&Thread.current[:test_block]) if Thread.current[:test_block] end
Use a context and inject its content at this place in the code
@param [String, Symbol] context_name The context namespace @param [*Any] args Any arg to be passed down to the injected context @param [String, Symbol] namespace namespace name where to look for the context @param [String, Symbol] ns Alias for :namespace @param block Content that will be re-injected (see execute_tests
)
# File lib/rspec_in_context/in_context.rb, line 84 def in_context(context_name, *args, namespace: nil, ns: nil, &block) namespace ||= ns Thread.current[:test_block] = block context_to_exec = InContext.find_context(context_name, namespace) return context { instance_exec(*args, &context_to_exec.block) } if context_to_exec.silent context(context_name.to_s) { instance_exec(*args, &context_to_exec.block) } end