will_paginateが正常に機能していることをテストするために、リクエストスペックを作成していますが、いくつか問題があります。まず、これが私の仕様の剪定バージョンです。
require 'spec_helper'
describe "Articles" do
subject { page }
describe "index page" do
let(:user) { FactoryGirl.create(:user) }
before { visit news_path }
describe "pagination" do
before(:all) { 31.times { FactoryGirl.create(:article, user: user) } }
after(:all) { Article.delete_all; User.delete_all }
let(:first_page) { Article.paginate(page: 1) }
let(:second_page) { Article.paginate(page: 2) }
it "should not list the second page of articles" do
second_page.each do |article|
page.should_not have_selector('li', text: article.title)
end
end
end
end
end
ご覧のとおり、ユーザーが記事のインデックスページにアクセスしたときに、記事の2ページ目が表示されないことを確認するテストがあります。このテストは失敗します:
1) Articles index page pagination should not list the second page of articles
Failure/Error: page.should_not have_selector('li', text: article.title)
expected css "li" with text "Article number 1" not to return anything
なぜこれが失敗しているのか理解できません。開発環境で31の記事を手動で作成し、ブラウザーで表示すると、ページネーションは正常に機能しますが、テスト環境に切り替えると、仕様が失敗します。
記事モデル:
class Article < ActiveRecord::Base
attr_accessible :body, :title
belongs_to :user
validates :user_id, presence: true
default_scope order: 'created_at DESC'
end
記事ファクトリーは次のようになります。
FactoryGirl.define do
factory :article do
sequence(:title) { |n| "Article number #{n}" }
body "This is the body"
user
end
end