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
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
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>: