RSpec: in-depth differences between before(:all) and before(:each)
Ok, so I've ran into a very strange issue, directly connected with before blocks. I'm doing a integration testing via Watir and RSpec. For a simple test to check if user can perform a login I'm creating a 'user' record in the db by means of factory_girl.
So I put the following code:
before(:each) do
@user = Factory(:user)
end
if "should perform a login" do
# do stuff
end
In do stuff
I call a browser and see how the user tries to login. Unfortunately, somehow he cannot do that — "Username isn't valid".
After some investigation I discovered that if I put the code for creating user in before(:all)
block, everything magically works. How's that? What's the difference between :all
and :each
in this context? Also, If I put the code for creating user actually in the test body, it still doesn't work (i.e. user somehow isn't added to the DB or something).
You probably have transactional fixtures enabled, thus your Watir process doesn't see database changes inside the transaction that each RSpec example is wrapped in.
Try disabling transactional features and use something like database cleaner to get a clean slate before each example.
精彩评论