user2952473
user2952473

Reputation: 99

Nested RSpec Tests

I am following Michael Hartl's tutorial and came across this following codes that I am having trouble comprehending.

  describe "index" do
    let(:user) { FactoryGirl.create(:user) }
    before(:each) do
      sign_in user
      visit users_path
    end

    it { should have_title('All users') }
    it { should have_content('All users') }

    describe "pagination" do

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

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

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

My question is: is this a NESTED TEST where the test block of Pagination runs inside Index Test block? in other words, the sequence of testing flow:

  1. before(:each) outer block of signing in user and visiting user path is executed
  2. then the inner block of 30.times { FactoryGirl.create(:user) is executed
  3. then the inner block of it { should have_selector('div.pagination') } is executed
  4. then the inner block of expect(page).to have_selector('li', text: user.name) is executed

thank you

Upvotes: 0

Views: 694

Answers (1)

CDub
CDub

Reputation: 13354

Here's the flow for the above test:

The before(:each) block is executed before each of the following:

it { should have_title('All users') }
it { should have_content('All users') }

Then, the before(:each) is executed again, followed by the describe block, which executes:

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

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

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

Finally, after(:all) { User.delete_all } is executed.

I hope this helps explain the flow.

Upvotes: 1

Related Questions