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

Instance Method Details

#define_context(context_name, namespace: nil, ns: nil, &block) ⇒ Object

Note:

contexts are scoped to the block they are defined in.

Let you define a context that can be reused later

Parameters:

  • context_name (String, Symbol)

    The name of the context that will be re-used later

  • namespace (String, Symbol) (defaults to: nil)

    namespace name where the context will be stored. It helps reducing colisions when you define “global” contexts

  • ns (String, Symbol) (defaults to: nil)

    Alias of namespace

  • block (Proc)

    Contain the code that will be injected with #in_context later



107
108
109
110
111
112
# File 'lib/rspec_in_context/in_context.rb', line 107

def define_context(context_name, namespace: nil, ns: nil, &block)
  namespace ||= ns
  instance_exec do
    InContext.add_context(context_name, hooks.instance_variable_get(:@owner), namespace, &block)
  end
end

#execute_testsObject Also known as: instanciate_context

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)



93
94
95
# File 'lib/rspec_in_context/in_context.rb', line 93

def execute_tests
  instance_exec(&Thread.current[:test_block]) if Thread.current[:test_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

Parameters:

  • context_name (String, Symbol)

    The context namespace

  • args (*Any)

    Any arg to be passed down to the injected context

  • namespace (String, Symbol) (defaults to: nil)

    namespace name where to look for the context

  • ns (String, Symbol) (defaults to: nil)

    Alias for :namespace

  • block

    Content that will be re-injected (see #execute_tests)



80
81
82
83
84
85
86
# File 'lib/rspec_in_context/in_context.rb', line 80

def in_context(context_name, *args, namespace: nil, ns: nil, &block)
  namespace ||= ns
  Thread.current[:test_block] = block
  context(context_name.to_s) do
    instance_exec(*args, &InContext.find_context(context_name, namespace).block)
  end
end