class Spectre::SpecContext

DSL Classes

Attributes

__after_blocks[R]
__before_blocks[R]
__desc[R]
__setup_blocks[R]
__subject[R]
__teardown_blocks[R]

Public Class Methods

new(subject, desc=nil) click to toggle source
# File lib/spectre.rb, line 282
def initialize subject, desc=nil
  @__subject = subject
  @__desc = desc

  @__before_blocks = []
  @__after_blocks = []
  @__setup_blocks = []
  @__teardown_blocks = []
end

Public Instance Methods

after(&block) click to toggle source
# File lib/spectre.rb, line 320
def after &block
  @__after_blocks << block
end
before(&block) click to toggle source
# File lib/spectre.rb, line 316
def before &block
  @__before_blocks << block
end
context(desc=nil, &block) click to toggle source
# File lib/spectre.rb, line 332
def context desc=nil, &block
  ctx = SpecContext.new(@__subject, desc)
  ctx._evaluate &block
end
it(desc, tags: [], with: [], &block) click to toggle source
# File lib/spectre.rb, line 292
def it desc, tags: [], with: [], &block

  # Get the file, where the spec is defined.
  # Nasty, but it works
  # Maybe there is another way, but this works for now
  spec_file = nil
  begin
    raise
  rescue => e
    spec_file = e.backtrace
      .select { |file| !file.include? 'lib/spectre' }
      .first
      .match(/(.*\.rb):\d+/)
      .captures
      .first
  end

  raise "`with' has to be an Array" unless with.is_a? Array

  data = with.map { |x| x.is_a?(Hash) ? OpenStruct.new(x) : x }

  @__subject.add_spec(desc, tags, data, block, self, spec_file)
end
setup(&block) click to toggle source
# File lib/spectre.rb, line 324
def setup &block
  @__setup_blocks << block
end
teardown(&block) click to toggle source
# File lib/spectre.rb, line 328
def teardown &block
  @__teardown_blocks << block
end