Question: Testing index view filtering in RSpec feature spec (Capybara)

Question

Testing index view filtering in RSpec feature spec (Capybara)

Answers 0
Added at 2017-07-24 19:07
Tags
Question

The index view of the "users" resource in a Rails 4.2 app is capable of filtering the displayed users by their role (such as in "admin" or "user") and country attributes.

The application uses Devise, so we need to create a user record in order to login. Note that the user created for login purposes inevitably appears in our test data.

I'd like your help to improve these tests (which are currently working) or to identify a better way to test a filter feature.

RSpec::Matchers.define :have_single_record do |id|
  match do |page|
    expect(page).to have_selector("tr.user", count: 1)
    expect(page).to have_selector("tr#user_#{id}")
  end
  failure_message do |page|
    "expected to find a single record in the page with id #{id}. " \
    "Found #{page.all("tr#user_#{id}").count} records instead."
  end
end

describe "User CRUD", type: :feature, js: true do
  # Create user for logging-in with Devise
  let!(:user) { FactoryGirl.create(:admin_user) }
  before(:each) { login_as(user) }

  describe "filtering" do
    it "filters by role" do
      # The logged in user has :admin role
      user = FactoryGirl.create(:user, role: :user)
      visit index_view_path
      select "user", from: "q_role_eq"
      # It auto-submits
      expect(page).to have_single_record(user.id)
    end

    it "filters by country" do
      # The country of the logged in user is "US"
      user = FactoryGirl.create(:user, country: "BR")
      visit index_view_path
      select "Brazil", from: "q_country_eq"
      # It auto-submits
      expect(page).to have_single_record(user.id)
    end
end

Thank you.

Answers to

Testing index view filtering in RSpec feature spec (Capybara)

Source Show
◀ Wstecz