Method: MiniSpec::ClassAPI#before

Defined in:
lib/minispec/api/class/before.rb

#before(*matchers, &proc) ⇒ Object

run some code before any or matching tests. if called without arguments the hook will run before any test. if any arguments passed it will run only before matched tests. strings, symbols and regexps accepted as arguments. also :except option accepted.

describe Specs do

  before /shoes/, except: /red/ do
    # ...
  end

end

Examples:

callback to run before any test

describe SomeTest do

  before do
    # ...
  end
end

callback to run only before :cart test

describe Specs do

  before :cart do
    # ...
  end

  testing :cart do
    # ...
  end
end

callback to run before any test that match /cart/

describe Specs do

  before /cart/ do
    # ...
  end

  testing :cart do
    # ...
  end
end

callback to run before any test that match /cart/ except :load_cart

describe Specs do

  before /cart/, except: :load_cart do
    # ...
  end

end

callback to run before any test that match /shoes/

but ones that match /red/


61
62
63
64
65
66
67
# File 'lib/minispec/api/class/before.rb', line 61

def before *matchers, &proc
  proc || raise(ArgumentError, 'block is missing')
  matchers.flatten!
  matchers = [:*] if matchers.empty?
  return if before?.find {|x| x[0] == matchers && x[1].source_location == proc.source_location}
  before?.push([matchers, proc])
end