在RSpec功能规范(Capybara)中测试索引视图过滤

时间:2017-07-24 17:14:05

标签: ruby-on-rails ruby testing rspec capybara

"用户"的index视图Rails 4.2应用程序中的资源能够按role(例如" admin"或" user")和country属性过滤显示的用户。

应用程序使用Devise,因此我们需要创建用户记录才能登录。请注意,为登录目的而创建的用户不可避免地出现在我们的测试数据中。

我希望您帮助改进这些测试(目前正在运行)或确定更好的方法来测试过滤器功能。

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

谢谢。

0 个答案:

没有答案