Class: Riot::Context

Inherits:
Object show all
Includes:
ContextClassOverrides, ContextHelpers, ContextOptions
Defined in:
lib/riot/context.rb

Overview

An Assertion is declared within a Context. The context stores setup and teardown blocks, and allows for nesting and refactoring. Extension developers may also configure Middleware objects in order to extend the functionality of a Context.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ContextHelpers

#asserts, #asserts_topic, #denies, #denies_topic, #helper, #hookup, #setup, #should, #should_not, #teardown

Methods included from ContextOptions

#option, #option_set, #set

Methods included from ContextClassOverrides

#assertion_class, #situation_class

Constructor Details

#initialize(description, parent = nil, &definition) ⇒ Context

Creates a new Context

Parameters:

  • description (String)

    a partial description of this context

  • parent (Riot::Context, nil) (defaults to: nil)

    a parent context or nothing

  • definition (lambda)

    the body of this context



50
51
52
53
54
55
56
57
58
59
# File 'lib/riot/context.rb', line 50

def initialize(description, parent=nil, &definition)
  @parent = parent || RootContext.new([],[], "", {})
  @description = description
  @contexts, @setups, @assertions, @teardowns = [], [], [], []
  @context_error = nil
  @options = @parent.option_set.dup
  prepare_middleware(&definition)
rescue Exception => e
  @context_error = e
end

Instance Attribute Details

#descriptionString (readonly)

The partial description of just this context.

Returns:

  • (String)


38
39
40
# File 'lib/riot/context.rb', line 38

def description
  @description
end

#parentRiot::Context? (readonly)

The parent context.

Returns:



43
44
45
# File 'lib/riot/context.rb', line 43

def parent
  @parent
end

Class Method Details

.middlewaresArray<Riot::ContextMiddleware>

The set of middleware helpers configured for the current test space.



33
# File 'lib/riot/context.rb', line 33

def self.middlewares; @middlewares ||= []; end

Instance Method Details

#context(description, &definition) ⇒ Riot::Context Also known as: describe

Create a new test context.

Parameters:

  • description (String)

Returns:



65
66
67
# File 'lib/riot/context.rb', line 65

def context(description, &definition)
  new_context(description, self.class, &definition)
end

#detailed_descriptionString

Prints the full description from the context tree, grabbing the description from the parent and appending the description given to this context.

Returns:

  • (String)

    the full description for this context



119
120
121
# File 'lib/riot/context.rb', line 119

def detailed_description
  "#{parent.detailed_description} #{description}".strip
end

#run(reporter) ⇒ Riot::Reporter

Executes the setups, hookups, assertions, and teardowns and passes results on to a given Reporter. Sub-contexts will also be executed and provided the given reporter. A new Situation will be created from the specified Situation class.

Parameters:

Returns:



92
93
94
95
96
97
98
99
100
101
# File 'lib/riot/context.rb', line 92

def run(reporter)
  reporter.describe_context(self) unless @assertions.empty?
  if @context_error
    reporter.report("context preparation", [:context_error, @context_error])
  else
    local_run(reporter, situation_class.new)
    run_sub_contexts(reporter)
  end
  reporter
end