Topic: before(:each) and before(:all) in rspec

Relatively new to rails and working my way through Michael Hartls tutorial. I'm stuck on some rspec code .... namely the meaning of before(:each) and before(:all) in the following snippet. My interpretation of whats happening in blue, for what its worth.

 describe "index" do

    let(:user) { FactoryGirl.create(:user) }

    before(:each) do
      sign_in user
      visit users_path

does this mean that the user is signed in before each of the following assertions. I.E. before it checks for title and before it checks for h1 and also before each of the assertions in the 'pagination' block below (including each of the assertions within the each block. which to my calulation involves 33 seperate sign ins and page visits. This doesn't sound right to me.

    it { should have_selector('title', text: 'All users') }
    it { should have_selector('h1',    text: 'All users') }

    describe "pagination" do

      before(:all) { 30.times { FactoryGirl.create(:user) } }
      after(:all)  { User.delete_all }

Does before(:all) mean that 30 factory users are created one time only before all of the assertions that follow, and that after all the assertions are completed all 30 factory users are deleted on one fell swoop.

  it { should have_selector('div.pagination') }

      it "should list each user" do
        User.paginate(page: 1).each do |user|
          page.should have_selector('li', text:

I haven't been able to find any clear explanation of this anywhere. Appreciate any help on this.

Re: before(:each) and before(:all) in rspec

The author use before(:each) to emphasize the contrast with before(:all). Following before(:each) there are only 2 test cases, so the sign_in action will happen 2 times.