Ask A Question

Notifications

You’re not receiving notifications from this thread.

How do I test the order of elements on a rails app page with Rspec / Capybara?

Micheal Waldrup asked in Testing

I have a 'Team' index page and I just want to TDD the ordering of the elements on page. My instructions are to validate they are showing most recently created first.

I know how to do the actual implementation, just not sure how to test that a team created today shows up before a team created yesterday.

Thanks!

Reply

I'm familiar with the nitty-gritty of putting this into practice, but I'm stumped as to how to guarantee that a team made today would appear ahead of one made yesterday during testing.

Reply

To test the order of elements on a page with Rspec and Capybara, you can do the following:

  1. Create a new Rspec test to check the display order of the teams: # spec/features/team_index_spec.rb require 'rails_helper'

RSpec.describe 'Team Index Page', type: :feature do
it 'displays teams in the correct order' do
# Create some teams with different creation dates
team1 = create(:team, created_at: 1.day.ago)
team2 = create(:team, created_at: Time.now)

# Visit the team index page
visit teams_path

# Check the display order of the teams
expect(page).to have_selector('div.team', count: 2)
expect(page.all('div.team')[0]).to have_content(team2.name)
expect(page.all('div.team')[1]).to have_content(team1.name)

end
end

  1. In the test above:
  2. Create two teams with different creation dates
  3. Visit the team index page
  4. Check that the page displays the correct 2 teams
  5. Check that the newly created team is displayed before the older one

  6. Run this test with rspec spec/features/team_index_spec.rb. If the test passes, it means the display order of the teams on the page is as expected.

I hope these instructions help you test the order of elements on a Rails app page with Rspec and Capybara.

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 85,376+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.