background VS. before(:each)

3,759 views
Skip to first unread message

Jaime Iniesta

unread,
Aug 25, 2010, 12:42:16 PM8/25/10
to ste...@googlegroups.com
Fellows, after googling and consulting the RSpec book I cannot answer
this doubt of mine, "when should we use background and when
before(:each)?". I can't tell really the best use for each one as both
will be executed before every single scenario.

To put an easy example, if you have a blog where an admin can create
posts and normal users can't, I'm using background to create some
sample posts and users with factories, and before(:each) to do the
admin login only on the given context:

###############################
feature "blog" do

  background do
    create_sample_posts
    create_sample_users
  end

  context "Authorized as admin" do
    before(:each) do
      admin_login
    end

    after(:each) do
      admin_logout
    end

    scenario "List posts" do
      visit "/posts"
      page.should have_content "Welcome to my blog"
      page.should have_content "First post"
      page.should have_content "Second post"
      page.should have_content "Third post"
      page.should have_link "New Post"
    end
    ...
  end

  context "Unauthorized" do
    scenario "List posts" do
      ...
      page.should_not have_link "New Post"
      ...
    end
  end
end
##################

I'm also using after(:each) to do the admin logout after every
scenario, because if I don't, session is maintained and the user is
still logged in.

What's the best way to do this? When to use background and when before(:each)?

A crucial difference is that before() has its counterpart, after();
but background does not have it (I was looking for the equivalent to
teardown in test unit).

Also, excuse me if this is not the right list to ask this question,
maybe I should write to the cucumber or rspec list instead?

Thanks!
Jaime

Luismi Cavallé

unread,
Aug 26, 2010, 2:58:59 PM8/26/10
to ste...@googlegroups.com
Technically `background` and `before(:each)` work exactly the same (`background` is an alias of `before` and `:each` is the default value). However my suggestion would be to use `background` inside the feature for context that is relevant to properly understand the rest of the feature, and `before` for plumbing or infrastructure code not particularly relevant (e.g. reseting database or sessions). Usually it'd be convenient to put your `before` blocks outside the features, i.e. in a helper file using `RSpec.configuration`.

In your example I'd use `background` instead of `before` and I'd move the `after` to a helper file (and I'd make sure it would logout any user, not only admins)

Summarizing:
- `background` in features for context relevant to understand the user story
- `before` and `after` outside features (in helper files) for plumbing or infrastructure code

Does it makes sense?

-- Luismi

Jaime Iniesta

unread,
Aug 26, 2010, 6:54:09 PM8/26/10
to ste...@googlegroups.com
Thanks Luismi.

It does make sense, I'll try this approach and see if it feels more
natural this way.

2010/8/26 Luismi Cavallé <lmca...@gmail.com>:

Reply all
Reply to author
Forward
0 new messages