简体   繁体   中英

RSpec test for Kaminari pagination doesn’t fail when it should

I want to test for Kaminari pagination with RSpec.

spec/requests/companies_spec.rb

require 'rails_helper'

describe "Companies" do
  describe "GET /companies" do
    before(:all) { 51.times { FactoryGirl.create(:company) }}
    describe "index" do
      it "has second page" do
        visit root_path
        find("//*[@class='pagination']//a[text()='2']").click
        expect(page.status_code).to eq(200)
      end
    end
  end
end

app/models/company.rb

class Company < ActiveRecord::Base
  paginates_per 50
end

This test creates 51 companies so that pagination for a second page is created. But even if I comment out before(:all) line, the test doesn't fail.

How can I write a test that works as I expect it to?

Make sure the created Company entries in your database are deleted after the test. If you don't, you will have more than 50 entries in the database from previous tests.

You can delete database entries after running tests by using the database_cleaner gem.

A test won't fails unless an expectation fails. Your only expecation is that the page.status_code is 200. So without the records what happens is

  1. You visit the root path (getting a 200)
  2. Capybara fails to find the 2 link and click on it
  3. Your test passes because you have successfully visited the root path

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM