RSpec: in-depth differences between before(:all) and before(:each)
        Posted  
        
            by gmile
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by gmile
        
        
        
        Published on 2010-04-08T14:41:02Z
        Indexed on 
            2010/04/08
            14:43 UTC
        
        
        Read the original article
        Hit count: 236
        
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).
© Stack Overflow or respective owner