before(:each) vs just before

rspec, ruby-on-rails

Solution

The `before` method accepts a `scope` parameter that defaults to `:each`. When you leave it out, it's implied that you mean `:each`, so your two examples do the exact same thing.

Here is a helpful tidbit from the RSpec RDoc, Module: RSpec::Core::Hooks#before:

Parameters:

- scope (Symbol) — `:each`, `:all`, or `:suite` (defaults to `:each`)

- conditions (Hash) — constrains this hook to examples matching these conditions e.g. `before(:each, :ui => true) { ... }` will only run with examples or groups declared with `:ui => true`.

Problem

I am new to ruby on rails. And playing around with testing Is there a difference between ``` before(:each) do #some test code end ``` and ``` before do #some test code end ```

Original source