Method: RSpec::Core::Hooks#before

Defined in:

#before(&block) ⇒ Object #before(scope, &block) ⇒ Object #before(scope, conditions, &block) ⇒ Object #before(conditions, &block) ⇒ Object Also known as: append_before


The `:example` and `:context` scopes are also available as `:each` and `:all`, respectively. Use whichever you prefer.


The `:suite` scope is only supported for hooks registered on `RSpec.configuration` since they exist independently of any example or example group.

Declare a block of code to be run before each example (using `:example`) or once before any example (using `:context`). These are usually declared directly in the ExampleGroup to which they apply, but they can also be shared across multiple groups.

You can also use `before(:suite)` to run a block of code before any example groups are run. This should be declared in RSpec.configure.

Instance variables declared in `before(:example)` or `before(:context)` are accessible within each example.

### Order

`before` hooks are stored in three scopes, which are run in order: `:suite`, `:context`, and `:example`. They can also be declared in several different places: `RSpec.configure`, a parent group, the current group. They are run in the following order:

before(:suite)    # Declared in RSpec.configure.
before(:context)  # Declared in RSpec.configure.
before(:context)  # Declared in a parent group.
before(:context)  # Declared in the current group.
before(:example)  # Declared in RSpec.configure.
before(:example)  # Declared in a parent group.
before(:example)  # Declared in the current group.

If more than one `before` is declared within any one scope, they are run in the order in which they are declared.

### Conditions

When you add a conditions hash to `before(:example)` or `before(:context)`, RSpec will only apply that hook to groups or examples that match the conditions. e.g.

RSpec.configure do |config|
  config.before(:example, :authorized => true) do

describe Something, :authorized => true do
  # The before hook will run in before each example in this group.

describe SomethingElse do
  it "does something", :authorized => true do
    # The before hook will run before this example.

  it "does something else" do
    # The hook will not run before this example.

Note that filtered config `:context` hooks can still be applied to individual examples that have matching metadata. Just like Ruby's object model is that every object has a singleton class which has only a single instance, RSpec's model is that every example has a singleton example group containing just the one example.

### Warning: `before(:suite, :with => :conditions)`

The conditions hash is used to match against specific examples. Since `before(:suite)` is not run in relation to any specific example or group, conditions passed along with `:suite` are effectively ignored.

### Exceptions

When an exception is raised in a `before` block, RSpec skips any subsequent `before` blocks and the example, but runs all of the `after(:example)` and `after(:context)` hooks.

### Warning: implicit before blocks

`before` hooks can also be declared in shared contexts which get included implicitly either by you or by extension libraries. Since RSpec runs these in the order in which they are declared within each scope, load order matters, and can lead to confusing results when one before block depends on state that is prepared in another before block that gets run later.

### Warning: `before(:context)`

It is very tempting to use `before(:context)` to speed things up, but we recommend that you avoid this as there are a number of gotchas, as well as things that simply don't work.

#### Context

`before(:context)` is run in an example that is generated to provide group context for the block.

#### Instance variables

Instance variables declared in `before(:context)` are shared across all the examples in the group. This means that each example can change the state of a shared object, resulting in an ordering dependency that can make it difficult to reason about failures.

#### Unsupported RSpec constructs

RSpec has several constructs that reset state between each example automatically. These are not intended for use from within `before(:context)`:

* `let` declarations
* `subject` declarations
* Any mocking, stubbing or test double declaration

### other frameworks

Mock object frameworks and database transaction managers (like ActiveRecord) are typically designed around the idea of setting up before an example, running that one example, and then tearing down. This means that mocks and stubs can (sometimes) be declared in `before(:context)`, but get torn down before the first real example is ever run.

You can create database-backed model objects in a `before(:context)` in rspec-rails, but it will not be wrapped in a transaction for you, so you are on your own to clean up in an `after(:context)` block.


before(:example) declared in an ExampleGroup

describe Thing do
  before(:example) do
    @thing =

  it "does something" do
    # Here you can access @thing.

before(:context) declared in an ExampleGroup

describe Parser do
  before(:context) do, 'w') do |f|
      f.write "stuff in the file\n"

  it "parses the file" do

  after(:context) do

See Also:

# File 'lib/rspec/core/hooks.rb', line 197

def before(*args, &block)
  hooks.register :append, :before, *args, &block